Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the lowest set bit

I have 5 bit numbers like

10000
01000
00100

If only one bit is on in my calculation i have no problem.

but if 2 bits are on then I want to select only the first on bit for example

10010

i want to treat it as 2 instead of the number 18

is there any bitwise operation which may i use in such sitution?

like image 520
user160820 Avatar asked Sep 03 '12 11:09

user160820


People also ask

How do you find the lowest set bit in a number?

Finding the lowest set bit turns out to be surprisingly easy, with the right combination of bitwise and arithmetic operators. Suppose we wish to find the lowest set bit of x (which is known to be non-zero). If we subtract 1 from x then this bit is cleared, but all the other one bits in x remain set.

What is the lowest set bit?

In computing, the least significant bit (LSB) is the bit position in a binary integer representing the binary 1s place of the integer. Similarly, the most significant bit (MSB) represents the highest-order place of the binary integer.

How do I find my LSB?

Logic to check Least Significant Bit (LSB) of a number To check LSB of a number we need to perform bitwise ANDing. The bitwise AND operation number & 1 will evaluate to 1 if LSB of number is set i.e. 1 otherwise evaluates to 0 . As you can see in above image 12 & 1 evaluate to 0 . Since, LSB of 12 is 0 .

How do you find the highest set bit in a number?

For finding the position of the leftmost set bit, we simply right shift the given number “N” till that number is > 0. Alongside, we increment the position variable to find the position of the leftmost set bit.


1 Answers

Since you only want to isolate it, not get its index, it's easy:

function firstSetBit(number)
{
    return number & -number;
}

It works because of the binary representation of -number, which is called "two's complement".

To get a better example, let's say the number is 888, which is 0000001101111000 in binary. The leading zeroes make a 16 bit number, but this works with any integer size.

To obtain the two's complement of a number, we first complement it, setting all 1s to 0s and 0s to 1s.

          number: 0000001101111000
      complement: 1111110010000111

Then we add 1 to it.

          number: 0000001101111000
      complement: 1111110010000111
           add 1: 1111110010001000

Note that if the rightmost bit is 1, this would create a carry which flips all 1s into 0s until a 0 is reached.

This number is now actually also the binary representation of -number.

          number: 0000001101111000
      complement: 1111110010000111
           add 1: 1111110010001000
         -number: 1111110010001000

We now take the bitwise & of number and -number.

          number: 0000001101111000
         -number: 1111110010001000
number & -number: 0000000000001000

To the right of the target bit, number is all 0s by premise. -number is also all 0s because they got flipped during the +1. Bitwise AND of 0 and 0 produces 0.

At the target bit, number has a 1, also by premise. -number also has a 1 because of the negate turning it into a 0 and carry putting it back to 1. Bitwise AND of 1 and 1 produces 1.

To the left of the target bit, number and -number always form 0 and 1 pairs because it is undisturbed by the +1 step of the two's complement procedure. Bitwise AND of 1 and 0 produces 0.

And thus, we have shown that number & -number produces the lowest 1 bit of the number.

like image 122
harold Avatar answered Oct 04 '22 04:10

harold