Find the lone set bit
You are given a single non-negative integer ‘N’ who’s binary representation consists of a single ‘1’ digit and the rest of the digits are ‘0’s. Your task is to find the position of the only ‘1’ digit. In other words, your task is to find the position of the only set bit in the binary representation of the input integer ‘N’.
The position of the set bit must be counted from the LSB (Least Significant Bit) end of the Binary number. If the count of set bits in the Binary number is not equal to 1, then your function should return ‘-1’ as output.
Example:-
INPUT : N = 4
OUTPUT : 3
In the above example, N = 4, whose Binary representation is “0100”. It is clearly visible that the binary number contains a single set bit, at a position 3 from the LSB end. Hence the output is 3
INPUT : N = 8
OUTPUT: 4
In the above example, N = 8, whose Binary representation is “1000”. It is clearly visible that the binary number contains a single set bit, at a position 4 from the LSB end. Hence the output is 4
INPUT : N = 9
OUTPUT : -1
In the above example, N = 9, whose Binary representation is “1001”. Now, the binary number contains 2 set bits, at a position 4 and 1 from LSB end. Hence the output is -1.
INPUT : N = 0
OUTPUT : -1
In the above example, N = 0, whose Binary representation is “0000”. Now, the binary number contains no set bits at all. Hence the output will be -1.
Input Format
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.
The first and the only line of each test case contains a single integer ‘N’.
Output Format:
For every test case, print a single integer, which is the position of the lone set bit in the binary representation of the input integer ‘N’.
Note:
You don’t have to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
0 <= N <= 10^5
Time limit: 1 second
CodingNinjas
author
2y
Approach 1
If we observe a little, we can clearly see that only those integers, which can be represented as a power of 2 have a single set bit in their binary equivalent, and rest either have 0 or mor...read more
CodingNinjas
author
2y
Approach 2
We know that the only set bit will always be the leftmost bit or the MSB (Most Significant Bit). So in this approach, we will perform the right shift operation on the only set bit of ‘N’ by ...read more
CodingNinjas
author
2y
Approach 3
If the integer is the power of 2, then finding the Log of the base 2, of the input integer N, and incrementing it by 1 will give the position of its set bit. If the integer is a power of two...read more
Add answer anonymously...
Top BugsMirror Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
>
BugsMirror Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app