- #1
andytran
- 41
- 0
Hi,
After hrs of googling still couldn't find the answer to my question. I'm hoping i get better luck positng it here. Any C/C++ out there please help!
1)
a) Given an unsigned int x, what does x &= x-1; do and why does it work?
I know what x &= x-1 does but to explain why does it work, i have no clue.
b) How can the statement in a) be used in a C++ program to count set bits (ones) in a given unsigned int x? What is the advantage over looping through all bit positions?
thanks in advance.
After hrs of googling still couldn't find the answer to my question. I'm hoping i get better luck positng it here. Any C/C++ out there please help!
1)
a) Given an unsigned int x, what does x &= x-1; do and why does it work?
I know what x &= x-1 does but to explain why does it work, i have no clue.
b) How can the statement in a) be used in a C++ program to count set bits (ones) in a given unsigned int x? What is the advantage over looping through all bit positions?
thanks in advance.