Position of rightmost set bit - GeeksforGeeks
Write a one line C function to return position of first 1 from right to left, in binary representation of an Integer.
http://algorithmsandme.com/2013/10/fun-with-bits-find-rightmost-bit-set-in-given-number/
Example : Let be the number be 18 = 00010010
Now if reset all bits except the LSB bit which is set and then take log (base 2), we can get the position of LSB which is set. We can do that by x & !(x-1)
log2(2) = 1, that is the position of LSB set.
Hence our return value becomes log2(x &!(x-1)
1. Take two's complement of the given no as all bits are reverted
except the first '1' from right to left (10111)
2 Do an bit-wise & with original no, this will return no with the
required one only (00010)
3 Take the log2 of the no, you will get position -1 (1)
4 Add 1 (2)
http://math-puzzles-computing.blogspot.com/2010/09/find-right-most-set-bit-in-integer.html
Use binary search to get logn.
Write a one line C function to return position of first 1 from right to left, in binary representation of an Integer.
http://algorithmsandme.com/2013/10/fun-with-bits-find-rightmost-bit-set-in-given-number/
Example : Let be the number be 18 = 00010010
Now if reset all bits except the LSB bit which is set and then take log (base 2), we can get the position of LSB which is set. We can do that by x & !(x-1)
log2(2) = 1, that is the position of LSB set.
Hence our return value becomes log2(x &!(x-1)
x-1 = 00010001!(x-1) = 11101110x & ! (x-1) = 00010010 & 11101110 = 0000010 =2
1. Take two's complement of the given no as all bits are reverted
except the first '1' from right to left (10111)
2 Do an bit-wise & with original no, this will return no with the
required one only (00010)
3 Take the log2 of the no, you will get position -1 (1)
4 Add 1 (2)
unsigned
int
getFirstSetBitPos(
int
n)
{
return
log2(n&-n)+1;
}
Use binary search to get logn.
int getPosition(unsigned x)
{
// ASSUMPTION: x will not be zero
// left, right and mid position
int l = 0, r = 33, mid = 0;
// temp value
unsigned temp;
// Iterate till we find the bit position
while(l < r)
{
// ((r - l) >> 1) + l - Is more effective??
mid = (l + r) >> 1;
// Set the most possible significant bit
temp = (1 << (mid - 1));
if(temp == x)
{
break;
}
else if(temp < x)
{
// Bit is in the higher half
l = mid;
}
else
{
// Bit is in the lower half
r = mid;
}
}
// Return mid itself if bits
// are positioned from 1 to 32
return (mid-1);
}
int getRightMostSetBit(unsigned x)
{
// Return 0 if x is zero
int position = 0;
// Avoid multiple return statements
if(x != 0)
{
// Make the integer passes as least power of 2
// Call the position locator
position = getPosition(x & -(signed)x);
}
return position;
}
public static long lssb(long x) { return Long.lowestOneBit(x); }http://www.informit.com/articles/article.aspx?p=1959565
Use the following formula to turn off the rightmost 1-bit in a word, producing 0 if none (e.g., 01011000 ⇒ 01010000):
x & (x – 1)
This can be used to determine if an unsigned integer is a power of 2 or is 0: apply the formula followed by a 0-test on the result.
Use the following formula to turn on the rightmost 0-bit in a word, producing all 1’s if none (e.g., 10100111 ⇒ 10101111):
x | (x+ 1)
Read full article from Position of rightmost set bit - GeeksforGeeks