cs_app 2.65 even_ones

/*二分的思路 很是巧妙*/

#include <stdio.h>
#include <stdlib.h>

int even_ones(unsigned x)
{
	int temp = x >> 16 ^ x;
	temp = temp >> 8 ^ temp;
	temp = temp >> 4 ^ temp;
	temp = temp >> 2 ^ temp;
	temp = temp >> 1 ^ temp;
	return !(temp & 1);
}
int main()
{
	unsigned a = 0;
	scanf("%d", &a);
	printf("%d\n", even_ones(a));
	return 0;
}

你可能感兴趣的:(ICS,CSAPP)