Print kth least significant bit of a number
Least significant number
As we know, each number can be represented in the form of digits, and the number format can be anything like binary, decimal, hexadecimal, octal, etc. If we represent the number in the form of bits, then the leftmost digit is called a most significant bit, and the rightmost digit is called a least significant bit.
As we know, the leftmost bit has a higher value as it is multiplied with the highest power of its base value, so it has the most significance in determining the value.
The rightmost digit is multiplied with zero power of base value, so it has the most minor contribution in the calculation of value. That is why it is called the least significant bit (LSB).
Let's take the number 12, so in the binary format; we can represent 12 in the form of 0s and 1s.
So binary representation of 12 will be: 1100
And the most significant bit of 12 is one, and the least significant bit is 0.
Now, we can find any least significant bit if we have binary representation on any number.
We will be given a number in integer and a number representing k. So, if we want a kth least significant bit, then we will simply get the binary representation of that particular number and store it into an array, then from the right-hand side kth element of the array will represent the kth least significant bit.
In the above, we used an array of size 32 to store the binary representation of a number n. The function 'getBinaryBits' will store the binary representing the number in binary format using the division method.
For example, we take n=27 and k=2.
So 27 in binary representation would like that:
So k=2 means that the 2nd least significant bit is one that is present at the second last index.
In the above code, we use extra space to store the bits, so time and space complexity would be O(32) which is nearly constant.
Another approach is that we will shift the bits to the right k-1 times.
As we know, each time when we shift an element, the least significant bit disappears, so by doing this operation k-1 times, we will remove the first k-1 bits from the left side, and the bit at the right most position would represent our least significant bit.
Now to determine if the bit is 1 or 0, we will check if the number is even. Then, the bit at the right-most is 0. Otherwise, it is 1.
The time complexity of the above code would be O(K), and the space complexity would be O(1).
This approach says that we will get the number where the kth bit will be one and the other bits will be zero. Now, if that number is bitwise AND with n, and if the result is zero, then it represents that the kth bit of n is zero. Otherwise, the kth bit of n is one.
To get the number that has the kth set bit and the other bit is zero, we will shift the 1 (k-1) times left.
Let's take n=16, and k=3, so we will rotate 1 (k-1), or we can say two times left.
So, we rotated the k, 2 times and did bitwise AND with n=16, and we got the result as 0. So the 3rd bit of n is definitely zero.
If we talk about time complexity, then it is also constant time and space complexity is also constant.
Time complexity: O(1)
Space complexity: O(1)