Here is a small piece of program which counts the number of bits set in a
number. I found it online somewhere.
InputOutput00(0000000)52(0000101)73(0000111)
*int* CountBits (*unsigned* *int* x )
{
*static* *unsigned* *int* mask[] *=* { 0x55555555,
0x33333333,
0x0F0F0F0F,
0x00FF00FF,
0x0000FFFF } ;
*int* i ;
*int* shift ; /* Number of positions to shift to right*/
*for* ( i *=*0, shift *=*1; i *<* 5; i *++*, shift **=* 2)
x *=* (x *&* mask[i ])*+* ( ( x *>>* shift) *&* mask[i]);
*return* x;
}
Can anyone explain how this is working?
--
Nikhil Gupta
Senior Co-ordinator, Publicity
CSI, NSIT Students' Branch
NSIT, New Delhi, India
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.