You are not logged in. Please login at www.codechef.com to post your questions!

×

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

bhanu1993's gravatar image

1★bhanu1993
3146
accept rate: 0%

edited 29 Jun '14, 12:39


@bhanu1993 Hi, I think following tutorial should help you. It also has other methods to count set bits in an integer.

Counting the number of set bits in an integer.

Thanks, Hope this helps
CrucifiX

link

answered 29 Jun '14, 13:38

crucifix's gravatar image

1★crucifix
7213810
accept rate: 18%

@Crucifix , this is the place i took it from , but unable to understand..

(29 Jun '14, 13:42) bhanu19931★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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