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!
Select
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
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits