> GJ wrote:[color=blue]
>
> I came accross the following code snippet to count the number of 1 bit in a
> word (32bit in this case). However I am not able to understand it thoroughly..
> Could anyone please explain this.
>
> ===============================================
>
> x = (0x55555555 & x) + (0x55555555 & (x>> 1));
>
> x = (0x33333333 & x) + (0x33333333 & (x>> 2));
>
> x = (0x0f0f0f0f & x) + (0x0f0f0f0f & (x>> 4));
>
> x = (0x00ff00ff & x) + (0x00ff00ff & (x>> 8));
>
> x = (0x0000ffff & x) + (0x0000ffff & (x>>16));
>
> ===============================================
>
> Regards,
>
> GJ[/color]
You will find this in "Hacker's Delight" by Warren on p. 65.
Basically it is divide-and-conquer. Warren explains it in detail.
--
Julian V. Noble
Professor Emeritus of Physics
jvn@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/
"For there was never yet philosopher that could endure the
toothache patiently."
-- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.