Found the function on “Computer Systems”:
int fun_a(unsigned x){
int val = 0;
while(x){
val ^= x;
x >>= 1;
}
return val & 0x1;
}
it return 1 if x contains odd “1”, return 0 if x contains even “1”.
I know “val & 0x1” check val is odd or even, but I don’t know how it count the bits number on val.
It doesn’t exactly count the bits, but the XOR will flip the low bit in
valonce for each bit inx. That will make it a1if it is flipped an odd number of times.It also flips a lot of other bits in
val, but that is irrelevant as they are masked off in the return statement.