# Smart way to count positive entries in bitset

This topic is 1930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I have an array of integers (let us assume here 32 bit ints).

[code]
N=SomeValue;
int *ptr=new int[N];
[/code]

Does there exist some clever way to sum all the ones (positive bits) instead of going trough every bit and converting it to bool, and if true then sum++?

##### Share on other sites
Aside from comparing every entry to 0 and then adding it, I don't see a better way unless you are able to store the entries sorted, so you only need to know where the first 0 is and then only iterate over a part of the array.

##### Share on other sites
You might be able to use the popcnt SSE4 instruction on each integer, which counts the number of bits set. Not every CPU supports it.
Otherwise, there are some portable tricks to counting number of bits, [url="http://en.wikipedia.org/wiki/Hamming_weight"]http://en.wikipedia.org/wiki/Hamming_weight[/url]

##### Share on other sites
Pity
I thought that it could work similarily to filling with ones or zeros the whole bitset. It is enough then to set ~0 or 0 to the whole container.

##### Share on other sites
Written in Notepad, it may or may not compile.
Also it's not tested, it may or may not be faster/slower, that's up to you to test.

[CODE]
// initialize table, could be constant, but that's a lot to type out
int numBits[256] = { };
for(int i = 0; i < 256; ++i) {
numBits[i] =
((i & 0x01) ? 1 : 0) +
((i & 0x02) ? 1 : 0) +
((i & 0x04) ? 1 : 0) +
((i & 0x08) ? 1 : 0) +
((i & 0xF0) ? 1 : 0) +
((i & 0xF1) ? 1 : 0) +
((i & 0xF2) ? 1 : 0) +
((i & 0xF4) ? 1 : 0) +
((i & 0xF8) ? 1 : 0);
}

int *ptr=new int[N];
int totalNumBits;
for(int i = 0; i < N; ++i) {
union {
int integer;
char charArray[4];
} int2char;
int2char.integer = ptr[i];
totalNumBits += numBits[int2char.charArray[0]] + numBits[int2char.charArray[1]] + numBits[int2char.charArray[2]] + numBits[int2char.charArray[3]];
}
[/CODE]

##### Share on other sites
I don't understand the question quite well, but if you are talking about count bit number in an integer, or any similar bit algorithm, here is a very good bit algorithm collection.
http://graphics.stanford.edu/~seander/bithacks.html
Bookmark/Delicious it if you didn't. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

##### Share on other sites
Apparently this is called "Hamming weight"

##### Share on other sites
[quote name='Ripiz' timestamp='1349433633' post='4987071']
Written in Notepad, it may or may not compile.
Also it's not tested, it may or may not be faster/slower, that's up to you to test.
[/quote]

It's also incorrect, you would need to test against:

0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80.

##### Share on other sites
This code (or similar) is somewhere in that bithacks page wqking linked:
[code]unsigned popcount(unsigned x) {
x -= (x >> 1) & 0x55555555u;
x = (x & 0x33333333u) + ((x >> 2) & 0x33333333u);
x = (x + (x >> 4)) & 0x0f0f0f0fu;
return (x * 0x01010101u) >> 24; // assumes unsigned is 32 bits
}
[/code]

gcc also has a function called __builtin_popcount, but on my computer the one above is three times faster. You can do it even faster using 64-bit arithmetic.

EDIT: If your hardware can take it, using __builtin_popcount and compiling with -msse4.2 issues the assembly instruction popcnt, which is extremely fast. Edited by alvaro

##### Share on other sites
[quote name='alvaro' timestamp='1349445503' post='4987115']
gcc also has a function called __builtin_popcount, but on my computer the one above is three times faster. You can do it even faster using 64-bit arithmetic.
[/quote]
Talking about performance, here is my personal experience (maybe off topic).
There is another lookup table algorithm for bit counting in my posted url, but be careful to use it.
AFAIR, it may be slower due to the data lookup may invalid the cpu data cache.
The algorithm posted by alvaro should be better because it uses "in-place" data and doesn't access any data in the memory. Edited by wqking

##### Share on other sites
@Alvaro & @wqking:

That rocks!!
Thanks a lot :]

##### Share on other sites
[quote name='alvaro' timestamp='1349445503' post='4987115']
This code (or similar) is somewhere in that bithacks page wqking linked:
[code]unsigned popcount(unsigned x) {
x -= (x >> 1) & 0x55555555u;
x = (x & 0x33333333u) + ((x >> 2) & 0x33333333u);
x = (x + (x >> 4)) & 0x0f0f0f0fu;
return (x * 0x01010101u) >> 24; // assumes unsigned is 32 bits
}
[/code]
[/quote]
That function scares me...

##### Share on other sites
[quote name='luca-deltodesco' timestamp='1349439130' post='4987085']
[quote name='Ripiz' timestamp='1349433633' post='4987071']
Written in Notepad, it may or may not compile.
Also it's not tested, it may or may not be faster/slower, that's up to you to test.
[/quote]

It's also incorrect, you would need to test against:

0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80.
[/quote]

Oh my... What have I done. Sometimes I wish I could downvote myself.