Find the Lone Set Bit
Your task is to identify the position of the only '1' bit in the binary representation of a given non-negative integer N
. The representation contains exactly one '1' and the rest are '0's. If there isn't exactly one '1' in the binary representation, the function should return -1
.
Input:
T : Number of test cases
N : Integer whose set bit position is to be found for each test case
Output:
Print a single integer for each test case indicating the position of the lone '1' bit, or -1 if there isn't exactly one '1'.
Example:
Input:
N = 4
Output:
3
Input:
N = 8
Output:
4
Input:
N = 9
Output:
-1
Input:
N = 0
Output:
-1
Constraints:
1 <= T <= 100
0 <= N <= 105
Note:
You do not need to print anything. Implement the function to return the correct value.

AnswerBot
4mo
Find the position of the lone '1' bit in the binary representation of a given non-negative integer.
Iterate through the bits of the integer to find the position of the lone '1'.
Use bitwise operations t...read more
Help your peers!
Add answer anonymously...
BugsMirror Software Developer Intern interview questions & answers
A Software Developer Intern was asked Q. What is the difference between deadlock and livelock?
A Software Developer Intern was asked Q. Find the Lone Set Bit Your task is to identify the position of the only '1' bit ...read more
A Software Developer Intern was asked Q. Ninja's Jump Task The Ninja has been given a challenge by his master to reach th...read more
Popular interview questions of Software Developer Intern
A Software Developer Intern was asked Q1. What is the difference between deadlock and livelock?
A Software Developer Intern was asked Q2. Find the Lone Set Bit Your task is to identify the position of the only '1' bit ...read more
A Software Developer Intern was asked Q3. Ninja's Jump Task The Ninja has been given a challenge by his master to reach th...read more
>
BugsMirror Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

