bitcount

Joined
Jun 17, 2009
Messages
2
Reaction score
0
i have been reading K&R but in this code i totally got lost i understand it counts the 1 in the binary but what i dont understand that how it does so because K&R didnt really explain it would be awesome if someone explained to me how it counts the 1z
Code:
/* bitcount: count 1 bits in x */
int bitcount(unsigned x)
{
int b;
for (b = 0; x != 0; x >>= 1)
if (x & 01)
b++;
return b;
}
 
Joined
Jun 17, 2009
Messages
2
Reaction score
0
i dont really understand why x!=0 and its alrdy an int not an string or something and when we shift it to the right it will only shift it how does it get the 1s ? then
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top