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
2d

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
Stay ahead in your career. Get AmbitionBox app
qr-code
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

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter