How to count bits in a unsigned int?

M

Mockey Chen

On 32-bit platform, give you a unsigned int?
what is the best way to get how many bits equal to 1.
please do not use loop if possible?

Thanks in advance.
 
M

mensanator

Mockey said:
On 32-bit platform, give you a unsigned int?
what is the best way to get how many bits equal to 1.
please do not use loop if possible?

Use the GMP library:

[Function] unsigned long int mpz popcount (mpz_t op)
If op  0, return the population count of op, which is the number of 1
bits in the binary
representation. If op < 0, the number of 1s is infinite, and the return
value is MAX ULONG,
the largest possible unsigned long.
 
K

Keith Thompson

Mockey Chen said:
On 32-bit platform, give you a unsigned int?
what is the best way to get how many bits equal to 1.
please do not use loop if possible?

The "please do not use loop if possible" clause makes this sound very
much like homework. If so, please give us your instructor's e-mail
address so we can submit the answer directly.
 
W

websnarf

Mockey said:
On 32-bit platform, give you a unsigned int?
what is the best way to get how many bits equal to 1.
please do not use loop if possible?

Look for "Pop Count" in AMD's Optimization documentation. They give a
very fast algorithm.
 
M

Mockey Chen

oops, homework? I never asked anyone to help me because of homework
when I was a student.

I saw this question in another bulletin board system. they guies give some
solutions for this topic, but not perfect, so I copy the question to here.

Since there are guru in this news group, I thought I can get a more better.
Now, I got a perfect answer is homework, haha. I become a student again.
 
C

Christian Bau

Keith Thompson said:
The "please do not use loop if possible" clause makes this sound very
much like homework. If so, please give us your instructor's e-mail
address so we can submit the answer directly.

But it is very simple (assuming thirtytwo bit unsigned int):

int bitcount (unsigned int x)
{
return
((x & (1u << 0)) != 0) +
((x & (1u << 1)) != 0) +
((x & (1u << 2)) != 0) +
((x & (1u << 3)) != 0) +
((x & (1u << 4)) != 0) +
((x & (1u << 5)) != 0) +
((x & (1u << 6)) != 0) +
((x & (1u << 7)) != 0) +
((x & (1u << 8)) != 0) +
((x & (1u << 9)) != 0) +
((x & (1u << 10)) != 0) +
((x & (1u << 11)) != 0) +
((x & (1u << 12)) != 0) +
((x & (1u << 13)) != 0) +
((x & (1u << 14)) != 0) +
((x & (1u << 15)) != 0) +
((x & (1u << 16)) != 0) +
((x & (1u << 17)) != 0) +
((x & (1u << 18)) != 0) +
((x & (1u << 19)) != 0) +
((x & (1u << 20)) != 0) +
((x & (1u << 21)) != 0) +
((x & (1u << 22)) != 0) +
((x & (1u << 23)) != 0) +
((x & (1u << 24)) != 0) +
((x & (1u << 25)) != 0) +
((x & (1u << 26)) != 0) +
((x & (1u << 27)) != 0) +
((x & (1u << 28)) != 0) +
((x & (1u << 29)) != 0) +
((x & (1u << 30)) != 0) +
((x & (1u << 31)) != 0);
}

No loop!
 
J

Jordan Abel

On 32-bit platform, give you a unsigned int?
what is the best way to get how many bits equal to 1.
please do not use loop if possible?

Thanks in advance.

Make a lookup table with 256 elements. check each 8-bit group in
turn. unsigned char weight[256] = {
0, 1, 1, 2, 1, 2, 2, 3,
}; etc
 
M

Mockey Chen

Excellent. thanks.

I find a solution in c++:

#include <bitset>
#include <iostream>
int main()
{
unsigned long x;
std::cin >> x;
std::bitset<32> bs(x);
std::cout << bs.count() << std::endl;
return 0;
}
 
R

Roberto Waltman

On 32-bit platform, give you a unsigned int?
what is the best way to get how many bits equal to 1.
please do not use loop if possible?

Thanks in advance.

The "best way" in my book uses a loop:

unsigned int bit_count(unsigned int n)
{
unsigned int count = 0;
while (n)
{
/* clear rightmost '1' bit */
n &= (n-1);
count++;
}
return count;
}

Ref: "Hacker's Delight"

Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]
 
E

Emmanuel Delahaye

Mockey Chen a écrit :
On 32-bit platform, give you a unsigned int?
what is the best way to get how many bits equal to 1.
please do not use loop if possible?

Use recursion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top