Fun with bits : Find rightmost bit set in given number | Algorithms and Me
Find the position of right most set bit
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))
Read full article from Fun with bits : Find rightmost bit set in given number | Algorithms and Me
No comments:
Post a Comment