Bit count: Parallel counting-mit hakmem

Source: Internet
Author: User

Q: Find the number of set bits in a given integer

Sol: Parallel Counting: MIT HAKMEM Count
Hakmem (hacks memo) is a legendary collection of neat mathematical and programming hacks contributed mostly by people at MIT and some elsewhere. this source is from the mit ai labs and this brilliant piece of code orginally in assembly was probably conceived
In the late 70's.

Int bitcount (unsigned int U)
{
Unsigned int ucount;

Ucount = u-(u> 1) & 033333333333)-(u> 2) & 011111111111 );
Return (ucount + (ucount> 3) & 030707070707) % 63;
}

Lets take a look at the theory behind this idea.

Take a 32bit number N;
N = A31 * 231 + A30 * 230 + ..... + AK * 2 K + .... + A1 * 2 + a0;

Here A0 through A31 are the values of BITs (0 or 1) in a 32 bit number. since the problem at hand is to count the number of set bits in the number, simply summing up these co-efficients wowould yeild the solution. (A0 + A1 + .. + A31 ).

How do we do this programmatically?

Take the original number N and store in the count variable.
Count = N;

Shift the orignal number 1 bit to the right and subtract from the orignal.
Count = N-(n> 1 );

Now shift the original number 2 bits to the right and subtract from count;
Count = N-(n> 1)-(N> 2 );

Keep doing this until you reach the end.
Count = N-(n> 1)-(N> 2 )-... -(N> 31 );

Let analyze and see what count holds now.
N = A31 * 231 + A30 * 230 + ..... + AK * 2 K + .... + A1 * 2 + a0;

N> 1 = A31 * 230 + A30 * 229 + ..... + AK * 2k-1 + .... + A1;
N> 2 = A31 * 229 + A30 * 228 + ..... + AK * 2k-2 + .... + A2

;
..
N> K = A31 * 2 (31-k) + A30 * 2 (30-k) + ..... + AK * 2 K;

..
N> 31 = A31;

You can quickly see that: (Hint: 2 k-2k-1 = 2k-1
Count = N-(n> 1)-(N> 2 )-... -(N> 31) = A31 + A30 +... + a0;
Which is what we are looking;

Int bitcount (unsigned int U)
{
Unsigned int ucount = u;
Do
{
U = u> 1;
Ucount-= u;
}
While (U );
}

This certainaly is an interesting way to solve this problem. But how do you make this brilliant? Run this in constant time with constant memory !!.

Int bitcount (unsigned int U)
{
Unsigned int ucount;

Ucount = u-(u> 1) & 033333333333)-(u> 2) & 011111111111 );
Return (ucount + (ucount> 3) & 030707070707) % 63;
}

For those of you who are still wondering whats going? Basically use the same idea, but instead of looping over the entire number, sum up the number in blocks of 3 (octal) and count them in parallel.

After this statement ucount = N-(n >>1) & 033333333333)-(n >> 2) & 011111111111 );
Ucount has the sum of BITs in each octal block spread out through itself.

So if you can a block of 3 bits

U = a2 * 22 + A1 * 2 + a0;
U> 1 = a2 * 2 + A1;
U> 2 = a2;

U-(u> 1)-(u> 2) is a2 + A1 + a0 which is the sum of BITs in each block of 3 bits.

The nexe step is to grab all these and sum them up:

(Ucount + (ucount> 3 )) will re-arrange them in blocks of 6 bits and sum them up in such a way the every other block of 3 has the sum of number of set bits in the original number plus the preceding block of 3. the only expection here is the first block
Of 3. the idea is not to spill over the bits to adjacent blocks while summing them up. how is that made possible. well, the maximum number of set bits in a block of 3 is 3, in a block of 6 is 6. and 3 bits can represent upto 7. this way you make sure you dont
Spill the bits over. To mask out the junk while doing a ucount> 3. Do and with 030707070707. The only expection is the first block as I just mentioned.

What does (ucount + (ucount> 3) & 030707070707) Hold now?
Its 2 ^ 0*(2 ^ 6-1) * sum0 + 2 ^ 1*(2 ^ 6-1) * sum1 + 2 ^ 2*(2 ^ 6-1) * sum2 + 2 ^ 3*(2 ^ 6-1) * sum3 + 2 ^ 4*(2 ^ 6-1) * sum4 + 2 ^ 5*(2 ^ 3-1) * sum5
Where sum0 is the sum of number of set bits in every block of 6 bits starting from the 'low' position.
What we need is sum0 + sum1 + sum2 + sum3 + sum4 + sum5;
2 ^ 6-1 is 63. Clearly a modulo with 63 will get you what you want.

Remember, that this works only with 32 bits numbers, for 64-bit numbers (well I will wait for comments or do this in the next post ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.