[algogeeks] Some bit manipulation help
Here is a small piece of program which counts the number of bits set in a number. I found it online somewhere. InputOutput00(000)52(101)73(111) *int* CountBits (*unsigned* *int* x ) { *static* *unsigned* *int* mask[] *=* { 0x, 0x, 0x0F0F0F0F, 0x00FF00FF, 0x } ; *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 algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Some bit manipulation help
Another approach!! while(x) { x=x(x-1); count++; } On Sun, Aug 28, 2011 at 2:26 PM, Nikhil Gupta nikhilgupta2...@gmail.comwrote: Here is a small piece of program which counts the number of bits set in a number. I found it online somewhere. InputOutput00(000)52(101)73(111) *int* CountBits (*unsigned* *int* x ) { *static* *unsigned* *int* mask[] *=* { 0x, 0x, 0x0F0F0F0F, 0x00FF00FF, 0x } ; *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 algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Some bit manipulation help
yeah..dat we all know..bt wat actually this code is doing..this is wat am thinking!!! On Sun, Aug 28, 2011 at 2:12 AM, Debabrata Das debabrata.barunhal...@gmail.com wrote: Another approach!! while(x) { x=x(x-1); count++; } On Sun, Aug 28, 2011 at 2:26 PM, Nikhil Gupta nikhilgupta2...@gmail.comwrote: Here is a small piece of program which counts the number of bits set in a number. I found it online somewhere. InputOutput00(000)52(101)73(111) *int* CountBits (*unsigned* *int* x ) { *static* *unsigned* *int* mask[] *=* { 0x, 0x, 0x0F0F0F0F, 0x00FF00FF, 0x } ; *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 algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- *Dheeraj Sharma* Comp Engg. NIT Kurukshetra +91 8950264227 -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Some bit manipulation help
Can anyone explain logic of the above code ? *Muthuraj R IV th Year , ISE PESIT , Bangalore* On Sun, Aug 28, 2011 at 2:16 AM, Dheeraj Sharma dheerajsharma1...@gmail.com wrote: yeah..dat we all know..bt wat actually this code is doing..this is wat am thinking!!! On Sun, Aug 28, 2011 at 2:12 AM, Debabrata Das debabrata.barunhal...@gmail.com wrote: Another approach!! while(x) { x=x(x-1); count++; } On Sun, Aug 28, 2011 at 2:26 PM, Nikhil Gupta nikhilgupta2...@gmail.comwrote: Here is a small piece of program which counts the number of bits set in a number. I found it online somewhere. InputOutput00(000)52(101)73(111) *int* CountBits (*unsigned* *int* x ) { *static* *unsigned* *int* mask[] *=* { 0x, 0x, 0x0F0F0F0F, 0x00FF00FF, 0x } ; *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 algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- *Dheeraj Sharma* Comp Engg. NIT Kurukshetra +91 8950264227 -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.