×

# counting set bits in a 32 bit number

 0 1  int NumberOfSetBits32(int i) { i = (i & 0x55555555) + ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i & 0x0F0F0F0F) + ((i >> 4) & 0x0F0F0F0F); return (i*(0x01010101))>>24; }  Can someone explain this code ,which finds the number of set bits in a number . I am completely unable to understand it. asked 29 Jun '14, 12:37 31●4●6 accept rate: 0%

 0 @bhanu1993 Hi, I think following tutorial should help you. It also has other methods to count set bits in an integer. Thanks, Hope this helps CrucifiX answered 29 Jun '14, 13:38 1★crucifix 721●3●8●10 accept rate: 18% @Crucifix , this is the place i took it from , but unable to understand.. (29 Jun '14, 13:42)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×77
×13
×1

question asked: 29 Jun '14, 12:37

question was seen: 808 times

last updated: 29 Jun '14, 13:42