| Thread Closed |
Bit Operation Counting |
Share Thread |
| Jan23-10, 04:52 PM | #1 |
|
|
Bit Operation Counting
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 |
| Jan25-10, 10:43 AM | #2 |
|
|
no loops or conditionals sounds unlikely. Not much can be done without 'if' and 'goto'.
|
| Jan25-10, 12:53 PM | #3 |
|
Recognitions:
|
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? |
| Jan29-10, 05:53 PM | #4 |
|
|
Bit Operation Counting
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.
|
| Jan29-10, 06:20 PM | #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.
|
| Jan30-10, 04:53 AM | #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.
|
| Jan30-10, 07:11 AM | #7 |
|
Recognitions:
|
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.
|
| Jan30-10, 07:42 AM | #8 |
|
|
Bit twiddling might be faster than the random access lookup into 2 GB too.
![]() 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; 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); |
| Feb2-10, 06:29 AM | #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 realised logical operators aren't really allowed i.e. a = b < 3 probably implies a conditional |
| Thread Closed |
Similar discussions for: Bit Operation Counting
|
||||
| Thread | Forum | Replies | ||
| closed operation | General Math | 3 | ||
| Simple Set Operation | Calculus & Beyond Homework | 1 | ||
| Matrix operation | Calculus & Beyond Homework | 5 | ||
| Binary operation | Set Theory, Logic, Probability, Statistics | 3 | ||
| Set operation problem | Set Theory, Logic, Probability, Statistics | 5 | ||