Solution Review: Get First Set Bit Position
Explore the method to identify the position of the first set bit in a number using bitwise right shift and AND operations. Understand the constant time algorithm and its implementation to enhance your problem-solving skills in bit manipulation.
We'll cover the following...
We'll cover the following...
Solution review
We check the right-most significant bit to see if the bit and & operation of 1 yields to 1.
In other words, we shift bits until the right MSB and & operation with 1 yields 1.
Algorithm
- If
n == 0, return; - Initialize
k = 1 - Loop
- If
((n >> (k - 1)) & 1) == 0
- If