Counting Bits

33,750 views

Published on

This ppt describes one of the interesting algorithms to count the number of bits set in an unsigned integer.
x = (x & 0x55555555) + ((x>>1)&0x55555555));
x = (x&0x33333333) + ((x>>2)&0x33333333);
...
....

2 Comments
3 Likes
Statistics
Notes
No Downloads
Views
Total views
33,750
On SlideShare
0
From Embeds
0
Number of Embeds
24,545
Actions
Shares
0
Downloads
186
Comments
2
Likes
3
Embeds 0
No embeds

No notes for slide
  • Counting Bits

    1. 1. Counting the number of bits set CH Gowri Kumar [email_address]
    2. 2. Copyright Notice <ul><li>© 2008, www.gowrikumar.com </li></ul><ul><li>You are given unrestricted freedom to </li></ul><ul><li>use, modify, copy and redistribute in </li></ul><ul><li>any format/media , as long as you retain </li></ul><ul><li>this copyright statement. </li></ul>
    3. 3. Problem Statement <ul><li>Given an unsigned integer n, count the number of bits set in it. </li></ul><ul><li>Ex : </li></ul><ul><ul><li>3  11  2 </li></ul></ul><ul><ul><li>8  1000  1 </li></ul></ul><ul><ul><li>23  10111  4 </li></ul></ul>
    4. 4. 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1
    5. 5. 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1
    6. 6. 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1
    7. 7. 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1
    8. 8. 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1
    9. 9. 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1
    10. 10. 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0
    11. 11. 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0
    12. 12. 0 1 0 0 0 1 1 0 0 0 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+ 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 0 1 0
    67. 67. + 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 1 0
    68. 68. 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0
    69. 69. + + 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0
    70. 70. 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0
    71. 71. C code <ul><li>x = (x & 0x5555) + ((x>>1) & 0x5555) </li></ul><ul><li>x = (x & 0x3333) + ((x>>2) & 0x3333) </li></ul><ul><li>x = (x & 0x0F0F) + ((x>>4) & 0x0F0F) </li></ul>
    72. 72. + 0 1 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0
    73. 73. + 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0
    74. 74. 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0
    75. 75. C code <ul><li>x = (x & 0x5555) + ((x>>1) & 0x5555) </li></ul><ul><li>x = (x & 0x3333) + ((x>>2) & 0x3333) </li></ul><ul><li>x = (x & 0x0F0F) + ((x>>4) & 0x0F0F) </li></ul><ul><li>x = (x & 0x00FF) + (x>>16) & 0x00FF) </li></ul>
    76. 76. Code for 32-bit integers <ul><li>x = (x&0x55555555) + ((x>>1)&0x55555555) </li></ul><ul><li>x = (x&0x33333333) + ((x>>2)&0x33333333) </li></ul><ul><li>x = (x&0x0F0F0F0F) + ((x>>4)&0x0F0F0F0F) </li></ul><ul><li>x = (x&0x00FF00FF) + ((x>>8)&0x00FF00FF) </li></ul><ul><li>x = (x&0x0000FFFF) + ((x>>16)&0x0000FFFF) </li></ul><ul><li>return x; </li></ul>
    77. 77. Reference <ul><li>Hackers Delight </li></ul>
    78. 78. The End

    ×