R
ravi
Hello,
the code below calculate the number of bits set in an unsigned
integer.
Can you explain me the logic behind the code?
unsigned int x = Some number;
x=(0xaaaaaaaa&x)>>1+(0x55555555&x);
x=(0xcccccccc&x)>>2+(0x33333333&x);
x=(0xf0f0f0f0&x)>>4+(0x0f0f0f0f&x);
x=(0xff00ff00&x)>>8+(0x00ff00ff&x);
x=x>>16+(0x0000ffff&x));
the code below calculate the number of bits set in an unsigned
integer.
Can you explain me the logic behind the code?
unsigned int x = Some number;
x=(0xaaaaaaaa&x)>>1+(0x55555555&x);
x=(0xcccccccc&x)>>2+(0x33333333&x);
x=(0xf0f0f0f0&x)>>4+(0x0f0f0f0f&x);
x=(0xff00ff00&x)>>8+(0x00ff00ff&x);
x=x>>16+(0x0000ffff&x));