Efficiently Count Bits in 32-bit Integers using Bit Operations

In summary, there is a way to count the number of bits to the right or left of a given 1 in a 32-bit integer, but it is not done using loops or conditionals. The catch is that this can be done efficiently, but you would need to know the layout of the data beforehand.
  • #1
noblerare
50
0
Hi all,

I am wondering is there a way to count the number of bits to the left or right of a given 1 in a 32-bit integer?

For example, if I give the function the number 32 = 0b100000, there are 5 bits to the right of the 1 and hence, 26 bits to the left of the 1.

The catch is, is there a way to do this by simply using bit operations? i.e. no loops or conditionals? Bit operations include: & ^ | << >> ! and ~

Thanks
 
Technology news on Phys.org
  • #2
no loops or conditionals sounds unlikely. Not much can be done without 'if' and 'goto'.
 
  • #3
noblerare said:
The catch is, is there a way to do this by simply using bit operations? i.e. no loops or conditionals? Bit operations include: & ^ | << >> ! and ~

Yes, as can all such functions. The question is whether this can be done efficiently.

I'm not entirely sure I understand your function, though. It looks like the first part is counting trailing zeros, and the second part is 31 minus the first part. Is that right?
 
  • #4
I have been looking into bitboards in chess programming a bit and found that some newer processors have assembly language instructions that might solve your problem, but I have no specifics.
 
  • #5
There are lots of tricks you can use with bit twiddling -- but your probably much better off learning how to invoke native operations rather than trying to roll your own (and you will probably get better performance too) -- functions like like popcnt, leadz and trailz.
 
  • #6
I would be curious to see the function -- given there are no loops or conditionals. Clearly that means no implied loops or conditionals (i.e. no function calls, intrinsics or conditional operators). I have heard it said you can do anything with a couple of logic operators but never seen a real example.
 
  • #7
You could split the number into two 16 bit values and use each value to index into one of two tables with 65536 entries. If you have the ram, a single look up into a table with 4gb entries would work, although it would take a bit to initialize the table.
 
  • #8
Bit twiddling might be faster than the random access lookup into 2 GB too. :wink:


If you really want a popcount function and your compiler doesn't offer you a builtin function to do it, you can do something like this:

Code:
uint32_t evens = x & 0x55555555;
uint32_t odds = x & 0xaaaaaaaa;
uint32_t two_long_counts = evens + (odds >> 1);
evens = two_long_counts & 0x33333333;
odds = two_long_counts & 0xcccccccc;
uint32_t four_long_counts = evens + (odds >> 2);
uint32_t eight_long_counts = four_long_counts + (four_long_counts) >> 4;
evens = eight_long_counts & 0x000f000f;
odds = eight_long_counts & 0x0f000f00;
uint32_t sixteen_long_counts = evens + (odds >> 8);
return (sixteen_long_counts + (sixteen_long_counts >> 16)) & 0x3f;

This computes the number of 1's in a 32-bit word. I think there are faster ways to do it too. This can be used to compute trailz:

Code:
uint32_t find_lowest_one = x & -x;
uint32_t make_all_smaller_bits_one = find_lowest_one - 1;
return popcount(make_all_smaller_bits_one);

You probably get better performance out of writing a divide-and-conquer version directly. A small lookup table can be useful to accelerate things -- also note you can do things like pack a 16-long lookup table of 2-bit entries into a single 32-bit word.
 
  • #9
Nice answer Hurkyl but not what the questioner wants - he wants the number of zeros to the right of the first one bit (and hence the number on the other side).

I thought I had figure it out but then realized logical operators aren't really allowed

i.e.

a = b < 3

probably implies a conditional
 

Related to Efficiently Count Bits in 32-bit Integers using Bit Operations

1. How does counting bits in 32-bit integers using bit operations work?

Bit operations involve manipulating individual bits (0s and 1s) within a binary number. In this case, we use bit operations to efficiently count the number of 1s (bits) in a 32-bit integer. This is done by using bitwise operators such as AND (&) and shift (<< and >>) to isolate and compare each bit in the integer.

2. Why is it important to efficiently count bits in 32-bit integers?

Counting bits in 32-bit integers is important in many applications, particularly in computer programming and data analysis. It allows us to quickly determine the number of 1s or 0s in a binary number, which can be useful for tasks such as error detection, data compression, and performance optimization.

3. How does counting bits using bit operations compare to other methods?

Counting bits using bit operations is generally considered to be the most efficient method, as it involves simple and fast operations that can be done in a single instruction. Other methods, such as using loops or built-in functions, may require more steps and be slower.

4. Can counting bits using bit operations be used for numbers other than 32-bit integers?

Yes, bit operations can be used for any type of integer, regardless of its size. However, the specific operations and number of bits may vary depending on the size of the integer. For example, for a 64-bit integer, we would need to use 64-bit operations instead of 32-bit ones.

5. Are there any potential drawbacks to using bit operations for counting bits?

One potential drawback is that bit operations may be more complex for those who are not familiar with binary numbers and bitwise operators. Additionally, if not used correctly, bit operations can lead to errors or unexpected results. It is important to thoroughly understand the concept before implementing it in a larger project.

Similar threads

  • Programming and Computer Science
Replies
1
Views
694
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
30
Views
4K
  • Computing and Technology
Replies
4
Views
856
  • Programming and Computer Science
Replies
3
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
7
Views
198
Back
Top