• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Find position of the only set bit

February 20, 2016 by Dhaval Dave

Given a number having only one ‘1’ and all other ’0’s in its binary representation, find position of the only set bit.

Last Asked at @Microsoft

Say number is : 5 so its binary is 0101 , so this number is not having only 1 and other 0
Lets Say number is 16 , so it binary is , 10000

So we can understand that if there is only 1 and rest all are 0 then the number should be perfect Square. ( this can be helpful in testing, or input test case).

ALGORTIHM :

1. Check if Number is power of two ( Else return )
2. Set two variables 'i'  for looping and 'pos' for position of set bit.
3. Run Loop, do boolean AND operation of 'i' and 'n' (input number). When result of this operation is one set the 'pos' variable.

OR

  1. We can Do right Shift Operation of number until we find 1.

CODE :

#include <stdio.h>
int isPowerOfTwo(unsigned n) { return (! (n & (n-1)) ); }
// Returns position of the only set bit in 'n'
int findPosition(unsigned n){
   if (!isPowerOfTwo(n))
   return -1;
   unsigned count = 0;

  // One by one move the only set bit to right till it reaches end
  while (n){
  n = n >> 1;

  // increment count of shifts
  ++count;
  }

return count;
}

int main(void) {
  int n = 4;
  int pos = findPosition(n);
  (pos == -1)? printf("n = %d, Invalid number\n", n):
  printf("n = %d, Position %d \n", n, pos);
  n = 5;
  pos = findPosition(n);
  (pos == -1)? printf("n = %d, Invalid number\n", n):
  printf("n = %d, Position %d \n", n, pos);

  n = 128;
  pos = findPosition(n);
  (pos == -1)? printf("n = %d, Invalid number\n", n):
  printf("n = %d, Position %d \n", n, pos);

  return 0;
}

Output:

n = 4, Position 3
n = 5, Invalid number
n = 128, Position 8

Similar Articles

Filed Under: Adobe Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Bit Arithmeticm

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Get K Max and Delete K Max in stream of incoming integers

25 horses 5 tracks Find 3 fastest puzzle

Knight Tour Problem (Graph – Breadth First Search)

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Stickler thief

Get Minimum element in O(1) from input numbers or Stack

Cisco Hiring Event 21st – 22nd Feb 2015

Sort Stack in place

DFS (Depth First Search)

The Magic HackerEarth Nirvana solutions Hiring Challenge

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Daughter’s Age VeryGood Puzzle

Interfaces in C++ (Abstract Classes in C++)

SAP Hiring Off-Campus General Aptitude

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Trapping Rain Water

Find the kth number with prime factors 3, 5 and 7

Implement LRU Cache

C++ OOPs Part1

Regular Expression Matching

Difference between a LinkedList and a Binary Search Tree BST

Convert number to words java

SAP Off Campus Hiring_ March 2015 Verbal Skills

Maximum path sum between two leaves

Find Pythagorean Triplets in an array in O(N)

Python Dictionaries

Print all nodes that are at distance k from a leaf node

Introduction To Number Theory ( Part 1 )

Count number of ways to reach a given score in a game

strtok()

Copyright © 2025 · Genesis Framework · WordPress · Log in