C / C++ FAQs & Programming Resources - ProkutFAQ : CountBits

HomePage Recent Changes Recently Commented Login/Register

Revision [633]

Most recent edit made on 2007-11-13 10:15:19 by SharathAV

Deletions:
elbobocvi




Revision [539]

Edited on 2007-11-09 14:20:53 by CaaceLdelc (unregistered user)

Additions:
elbobocvi




Revision [264]

Edited on 2006-12-19 06:39:40 by SharathAV

Additions:

How to count the number of 1's (set bits) in a given number ?

There are many ways you can do this, below are a few naive methods for the beginners:
Please refer to this site Bit Twiddling Hacks to learn many other methods to count bits, which are much more efficient than the above methods, and they manipulate bits at the lowest level.


Deletions:
Counting the number of bits that are set in a given number
There are many ways you can do this.
All the methods that I have mentioned here are naive attempts and are very easy to understand. Please refer to this site Bit Twiddling Hacks to learn more about manipulating bits at the lowest level.




Revision [244]

Edited on 2006-12-11 20:31:02 by PriyaSridharan

Additions:

int count= 0;
while(num){
count += num & 1;
num
= 1;
}
return count;


Deletions:

int count= 0;
while(num){
count += num & 1;
num
= 1;
}
return count;




Revision [243]

Edited on 2006-12-11 20:30:22 by PriyaSridharan

Additions:

int count= 0;
while(num){
count += num & 1;
num
= 1;
}
return count;
} %%


Deletions:

int count=num&1;
while(num
=1){
(num&1)?++count:count;




Revision [197]

Edited on 2006-11-09 03:19:51 by PriyaSridharan

Additions:

while(num
=1){


Deletions:

while(num
1){




Revision [44]

Edited on 2006-10-11 07:29:35 by PriyaSridharan [Edited by Sacred Feminine aka Priya]

Additions:
All the methods that I have mentioned here are naive attempts and are very easy to understand. Please refer to this site Bit Twiddling Hacks to learn more about manipulating bits at the lowest level.


Deletions:
All the methods that I have mentioned here are naive attempts and are very easy to understand. Please refer to this site Bit Twiddling Hacks to learn more about bit twiddling hacks.




Revision [36]

Edited on 2006-10-11 06:02:29 by PriyaSridharan

Additions:
Counting the number of bits that are set in a given number




Revision [33]

Edited on 2006-10-11 05:28:12 by PriyaSridharan [Priya]

Additions:
    while(num>>1){
         (num&1)?++count:count;
(c)
Here is how you can do it in C++ using the bitset class provided in the STL.
All the methods that I have mentioned here are naive attempts and are very easy to understand. Please refer to this site Bit Twiddling Hacks to learn more about bit twiddling hacks.


Deletions:

while((num
1) & 1){
count++;
All the methods that I have mentioned here are naive attempts. Please refer to this site to learn more about bitt twiddling hacks.
Bit Twiddling Hacks




Revision [32]

Edited on 2006-10-11 05:14:47 by PriyaSridharan

Additions:
int countBits(int num){
std::bitset<sizeof(int)*8> temp(num);
return temp.count();


Deletions:





Revision [31]

Edited on 2006-10-11 05:13:42 by PriyaSridharan

Additions:

All the methods that I have mentioned here are naive attempts. Please refer to this site to learn more about bitt twiddling hacks.
Bit Twiddling Hacks


Deletions:
bitset<sizeof(int)*8> temp(num);
return temp.count();




Revision [30]

Edited on 2006-10-11 05:02:23 by PriyaSridharan

Additions:
%%(cpp)


Deletions:
%%(cpp;1)




Revision [29]

The oldest known version of this page was edited on 2006-10-11 05:01:58 by PriyaSridharan
This is another question that confounds most of the newbies in Orkut and they end up confusing themselves and the others.

There are many ways you can do this.

  1. int countBits(unsigned num){
  2. bitset<sizeof(int)*8> temp(num);
  3. return temp.count();
  4. }
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.1681 seconds