Given a number having only one ‘1’ and all other ’0’s in its binary representation, find position of the only set bit. Source: Microsoft Interview | 18
The idea is to start from the rightmost bit and one by one check value of every bit. Following is a detailed algorithm.
1) If number is power of two then and then only its binary representation contains only one ‘1’. That’s why check whether the given number is a power of 2 or not. If given number is not a power of 2, then print error message and exit.
2) Initialize two variables; i = 1 (for looping) and pos = 1 (to find position of set bit)
3) Inside loop, do bitwise AND of i and number ‘N’. If value of this operation is true, then “pos” bit is set, so break the loop and return position. Otherwise, increment “pos” by 1 and left shift i by 1 and repeat the procedure.
C++
#include <bits/stdc++.h>
using namespace std;
int isPowerOfTwo(unsigned n)
{
return n && (!(n & (n - 1)));
}
int findPosition(unsigned n)
{
if (!isPowerOfTwo(n))
return -1;
unsigned i = 1, pos = 1;
while (!(i & n)) {
i = i << 1;
++pos;
}
return pos;
}
int main( void )
{
int n = 16;
int pos = findPosition(n);
(pos == -1) ? cout << "n = " << n << ", Invalid number" << endl : cout << "n = " << n << ", Position " << pos << endl;
n = 12;
pos = findPosition(n);
(pos == -1) ? cout << "n = " << n << ", Invalid number" << endl : cout << "n = " << n << ", Position " << pos << endl;
n = 128;
pos = findPosition(n);
(pos == -1) ? cout << "n = " << n << ", Invalid number" << endl : cout << "n = " << n << ", Position " << pos << endl;
return 0;
}
|
C
#include <stdio.h>
int isPowerOfTwo(unsigned n)
{
return n && (!(n & (n - 1)));
}
int findPosition(unsigned n)
{
if (!isPowerOfTwo(n))
return -1;
unsigned i = 1, pos = 1;
while (!(i & n)) {
i = i << 1;
++pos;
}
return pos;
}
int main( void )
{
int n = 16;
int pos = findPosition(n);
(pos == -1) ? printf ( "n = %d, Invalid number\n" , n) : printf ( "n = %d, Position %d \n" , n, pos);
n = 12;
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;
}
|
Java
class GFG {
static boolean isPowerOfTwo( int n)
{
return (n > 0 && ((n & (n - 1 )) == 0 )) ? true : false ;
}
static int findPosition( int n)
{
if (!isPowerOfTwo(n))
return - 1 ;
int i = 1 , pos = 1 ;
while ((i & n) == 0 ) {
i = i << 1 ;
++pos;
}
return pos;
}
public static void main(String[] args)
{
int n = 16 ;
int pos = findPosition(n);
if (pos == - 1 )
System.out.println( "n = " + n + ", Invalid number" );
else
System.out.println( "n = " + n + ", Position " + pos);
n = 12 ;
pos = findPosition(n);
if (pos == - 1 )
System.out.println( "n = " + n + ", Invalid number" );
else
System.out.println( "n = " + n + ", Position " + pos);
n = 128 ;
pos = findPosition(n);
if (pos == - 1 )
System.out.println( "n = " + n + ", Invalid number" );
else
System.out.println( "n = " + n + ", Position " + pos);
}
}
|
Python3
def isPowerOfTwo(n):
return ( True if (n > 0 and
((n & (n - 1 )) > 0 ))
else False );
def findPosition(n):
if (isPowerOfTwo(n) = = True ):
return - 1 ;
i = 1 ;
pos = 1 ;
while ((i & n) = = 0 ):
i = i << 1 ;
pos + = 1 ;
return pos;
n = 16 ;
pos = findPosition(n);
if (pos = = - 1 ):
print ( "n =" , n, ", Invalid number" );
else :
print ( "n =" , n, ", Position " , pos);
n = 12 ;
pos = findPosition(n);
if (pos = = - 1 ):
print ( "n =" , n, ", Invalid number" );
else :
print ( "n =" , n, ", Position " , pos);
n = 128 ;
pos = findPosition(n);
if (pos = = - 1 ):
print ( "n =" , n, ", Invalid number" );
else :
print ( "n =" , n, ", Position " , pos);
|
C#
using System;
class GFG {
static bool isPowerOfTwo( int n)
{
return (n > 0 && ((n & (n - 1)) == 0)) ? true : false ;
}
static int findPosition( int n)
{
if (!isPowerOfTwo(n))
return -1;
int i = 1, pos = 1;
while ((i & n) == 0) {
i = i << 1;
++pos;
}
return pos;
}
static void Main()
{
int n = 16;
int pos = findPosition(n);
if (pos == -1)
Console.WriteLine( "n = " + n + ", Invalid number" );
else
Console.WriteLine( "n = " + n + ", Position " + pos);
n = 12;
pos = findPosition(n);
if (pos == -1)
Console.WriteLine( "n = " + n + ", Invalid number" );
else
Console.WriteLine( "n = " + n + ", Position " + pos);
n = 128;
pos = findPosition(n);
if (pos == -1)
Console.WriteLine( "n = " + n + ", Invalid number" );
else
Console.WriteLine( "n = " + n + ", Position " + pos);
}
}
|
PHP
<?php
function isPowerOfTwo( $n )
{
return $n && (!( $n & ( $n - 1)));
}
function findPosition( $n )
{
if (!isPowerOfTwo( $n ))
return -1;
$i = 1;
$pos = 1;
while (!( $i & $n ))
{
$i = $i << 1;
++ $pos ;
}
return $pos ;
}
$n = 16;
$pos = findPosition( $n );
if (( $pos == -1) == true)
echo "n =" , $n , ", " ,
" Invalid number" , "\n" ;
else
echo "n = " , $n , ", " ,
" Position " , $pos , "\n" ;
$n = 12;
$pos = findPosition( $n );
if (( $pos == -1) == true)
echo "n = " , $n , ", " ,
" Invalid number" , "\n" ;
else
echo "n =" , $n , ", " ,
" Position " , $pos , "\n" ;
$n = 128;
$pos = findPosition( $n );
if (( $pos == -1) == true)
echo "n =" , $n , ", " ,
" Invalid number" , "\n" ;
else
echo "n = " , $n , ", " ,
" Position " , $pos , "\n" ;
?>
|
Output :
n = 16, Position 5
n = 12, Invalid number
n = 128, Position 8
Following is another method for this problem. The idea is to one by one right shift the set bit of given number ‘n’ until ‘n’ becomes 0. Count how many times we shifted to make ‘n’ zero. The final count is the position of the set bit.
C++
#include <bits/stdc++.h>
using namespace std;
int isPowerOfTwo(unsigned n)
{
return n && (!(n & (n - 1)));
}
int findPosition(unsigned n)
{
if (!isPowerOfTwo(n))
return -1;
unsigned count = 0;
while (n)
{
n = n >> 1;
++count;
}
return count;
}
int main( void )
{
int n = 0;
int pos = findPosition(n);
(pos == -1) ? cout<< "n = " <<n<< ", Invalid number\n" :
cout<< "n = " <<n<< ", Position " << pos<<endl;
n = 12;
pos = findPosition(n);
(pos == -1) ? cout<< "n = " <<n<< ", Invalid number\n" :
cout<< "n = " <<n<< ", Position " << pos<<endl;
n = 128;
pos = findPosition(n);
(pos == -1) ? cout<< "n = " <<n<< ", Invalid number\n" :
cout<< "n = " <<n<< ", Position " << pos<<endl;
return 0;
}
|
C
#include <stdio.h>
int isPowerOfTwo(unsigned n)
{
return n && (!(n & (n - 1)));
}
int findPosition(unsigned n)
{
if (!isPowerOfTwo(n))
return -1;
unsigned count = 0;
while (n) {
n = n >> 1;
++count;
}
return count;
}
int main( void )
{
int n = 0;
int pos = findPosition(n);
(pos == -1) ? printf ( "n = %d, Invalid number\n" , n) : printf ( "n = %d, Position %d \n" , n, pos);
n = 12;
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;
}
|
Java
class GFG {
static boolean isPowerOfTwo( int n)
{
return n > 0 && ((n & (n - 1 )) == 0 );
}
static int findPosition( int n)
{
if (!isPowerOfTwo(n))
return - 1 ;
int count = 0 ;
while (n > 0 ) {
n = n >> 1 ;
++count;
}
return count;
}
public static void main(String[] args)
{
int n = 0 ;
int pos = findPosition(n);
if (pos == - 1 )
System.out.println( "n = " + n + ", Invalid number" );
else
System.out.println( "n = " + n + ", Position " + pos);
n = 12 ;
pos = findPosition(n);
if (pos == - 1 )
System.out.println( "n = " + n + ", Invalid number" );
else
System.out.println( "n = " + n + ", Position " + pos);
n = 128 ;
pos = findPosition(n);
if (pos == - 1 )
System.out.println( "n = " + n + ", Invalid number" );
else
System.out.println( "n = " + n + ", Position " + pos);
}
}
|
Python3
def isPowerOfTwo(n) :
return (n and ( not (n & (n - 1 ))))
def findPosition(n) :
if not isPowerOfTwo(n) :
return - 1
count = 0
while (n) :
n = n >> 1
count + = 1
return count
if __name__ = = "__main__" :
n = 0
pos = findPosition(n)
if pos = = - 1 :
print ( "n =" , n, "Invalid number" )
else :
print ( "n =" , n, "Position" , pos)
n = 12
pos = findPosition(n)
if pos = = - 1 :
print ( "n =" , n, "Invalid number" )
else :
print ( "n =" , n, "Position" , pos)
n = 128
pos = findPosition(n)
if pos = = - 1 :
print ( "n =" , n, "Invalid number" )
else :
print ( "n =" , n, "Position" , pos)
|
C#
using System;
class GFG {
static bool isPowerOfTwo( int n)
{
return n > 0 && ((n & (n - 1)) == 0);
}
static int findPosition( int n)
{
if (!isPowerOfTwo(n))
return -1;
int count = 0;
while (n > 0) {
n = n >> 1;
++count;
}
return count;
}
static void Main()
{
int n = 0;
int pos = findPosition(n);
if (pos == -1)
Console.WriteLine( "n = " + n + ", Invalid number" );
else
Console.WriteLine( "n = " + n + ", Position " + pos);
n = 12;
pos = findPosition(n);
if (pos == -1)
Console.WriteLine( "n = " + n + ", Invalid number" );
else
Console.WriteLine( "n = " + n + ", Position " + pos);
n = 128;
pos = findPosition(n);
if (pos == -1)
Console.WriteLine( "n = " + n + ", Invalid number" );
else
Console.WriteLine( "n = " + n + ", Position " + pos);
}
}
|
PHP
<?php
function isPowerOfTwo( $n )
{
return $n && (! ( $n & ( $n - 1)));
}
function findPosition( $n )
{
if (!isPowerOfTwo( $n ))
return -1;
$count = 0;
while ( $n )
{
$n = $n >> 1;
++ $count ;
}
return $count ;
}
$n = 0;
$pos = findPosition( $n );
if (( $pos == -1) == true)
echo "n = " , $n , ", " ,
" Invalid number" , "\n" ;
else
echo "n = " , $n , ", " ,
" Position " , $pos , "\n" ;
$n = 12;
$pos = findPosition( $n );
if (( $pos == -1) == true)
echo "n = " , $n , ", " ,
" Invalid number" , "\n" ;
else
echo "n = " , $n ,
" Position " , $pos , "\n" ;
$n = 128;
$pos = findPosition( $n );
if (( $pos == -1) == true)
echo "n = " , $n , ", " ,
" Invalid number" , "\n" ;
else
echo "n = " , $n , ", " ,
" Position " , $pos , "\n" ;
?>
|
Output :
n = 0, Invalid number
n = 12, Invalid number
n = 128, Position 8
We can also use log base 2 to find the position. Thanks to Arunkumar for suggesting this solution.
C++
#include <bits/stdc++.h>
using namespace std;
unsigned int Log2n(unsigned int n)
{
return (n > 1) ? 1 + Log2n(n / 2) : 0;
}
int isPowerOfTwo(unsigned n)
{
return n && (!(n & (n - 1)));
}
int findPosition(unsigned n)
{
if (!isPowerOfTwo(n))
return -1;
return Log2n(n) + 1;
}
int main( void )
{
int n = 0;
int pos = findPosition(n);
(pos == -1) ? cout<< "n = " <<n<< ", Invalid number\n" :
cout<< "n = " <<n<< ", Position " <<pos<< " \n" ;
n = 12;
pos = findPosition(n);
(pos == -1) ? cout<< "n = " <<n<< ", Invalid number\n" :
cout<< "n = " <<n<< ", Position " <<pos<< " \n" ;
n = 128;
pos = findPosition(n);
(pos == -1) ? cout<< "n = " <<n<< ", Invalid number\n" :
cout<< "n = " <<n<< ", Position " <<pos<< " \n" ;
return 0;
}
|
C
#include <stdio.h>
unsigned int Log2n(unsigned int n)
{
return (n > 1) ? 1 + Log2n(n / 2) : 0;
}
int isPowerOfTwo(unsigned n)
{
return n && (!(n & (n - 1)));
}
int findPosition(unsigned n)
{
if (!isPowerOfTwo(n))
return -1;
return Log2n(n) + 1;
}
int main( void )
{
int n = 0;
int pos = findPosition(n);
(pos == -1) ? printf ( "n = %d, Invalid number\n" , n) : printf ( "n = %d, Position %d \n" , n, pos);
n = 12;
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;
}
|
Java
class GFG {
static int Log2n( int n)
{
return (n > 1 ) ? 1 + Log2n(n / 2 ) : 0 ;
}
static boolean isPowerOfTwo( int n)
{
return n > 0 && ((n & (n - 1 )) == 0 );
}
static int findPosition( int n)
{
if (!isPowerOfTwo(n))
return - 1 ;
return Log2n(n) + 1 ;
}
public static void main(String[] args)
{
int n = 0 ;
int pos = findPosition(n);
if (pos == - 1 )
System.out.println( "n = " + n + ", Invalid number " );
else
System.out.println( "n = " + n + ", Position " + pos);
n = 12 ;
pos = findPosition(n);
if (pos == - 1 )
System.out.println( "n = " + n + ", Invalid number " );
else
System.out.println( "n = " + n + ", Position " + pos);
n = 128 ;
pos = findPosition(n);
if (pos == - 1 )
System.out.println( "n = " + n + ", Invalid number " );
else
System.out.println( "n = " + n + ", Position " + pos);
}
}
|
Python3
def Log2n(n):
if (n > 1 ):
return ( 1 + Log2n(n / 2 ))
else :
return 0
def isPowerOfTwo(n):
return n and ( not (n & (n - 1 )) )
def findPosition(n):
if ( not isPowerOfTwo(n)):
return - 1
return Log2n(n) + 1
n = 0
pos = findPosition(n)
if (pos = = - 1 ):
print ( "n =" , n, ", Invalid number" )
else :
print ( "n = " , n, ", Position " , pos)
n = 12
pos = findPosition(n)
if (pos = = - 1 ):
print ( "n =" , n, ", Invalid number" )
else :
print ( "n = " , n, ", Position " , pos)
n = 128
pos = findPosition(n)
if (pos = = - 1 ):
print ( "n = " , n, ", Invalid number" )
else :
print ( "n = " , n, ", Position " , pos)
|
C#
using System;
class GFG {
static int Log2n( int n)
{
return (n > 1) ? 1 + Log2n(n / 2) : 0;
}
static bool isPowerOfTwo( int n)
{
return n > 0 && ((n & (n - 1)) == 0);
}
static int findPosition( int n)
{
if (!isPowerOfTwo(n))
return -1;
return Log2n(n) + 1;
}
static void Main()
{
int n = 0;
int pos = findPosition(n);
if (pos == -1)
Console.WriteLine( "n = " + n + ", Invalid number " );
else
Console.WriteLine( "n = " + n + ", Position " + pos);
n = 12;
pos = findPosition(n);
if (pos == -1)
Console.WriteLine( "n = " + n + ", Invalid number " );
else
Console.WriteLine( "n = " + n + ", Position " + pos);
n = 128;
pos = findPosition(n);
if (pos == -1)
Console.WriteLine( "n = " + n + ", Invalid number " );
else
Console.WriteLine( "n = " + n + ", Position " + pos);
}
}
|
PHP
<?php
function Log2n( $n )
{
return ( $n > 1) ? 1 +
Log2n( $n / 2) : 0;
}
function isPowerOfTwo( $n )
{
return $n && (! ( $n &
( $n - 1)));
}
function findPosition( $n )
{
if (!isPowerOfTwo( $n ))
return -1;
return Log2n( $n ) + 1;
}
$n = 0;
$pos = findPosition( $n );
if (( $pos == -1) == true)
echo "n =" , $n , ", " ,
" Invalid number" , "\n" ;
else
echo "n = " , $n , ", " ,
" Position n" , $pos , "\n" ;
$n = 12;
$pos = findPosition( $n );
if (( $pos == -1) == true)
echo "n = " , $n , ", " ,
" Invalid number" , "\n" ;
else
echo "n =" , $n , ", " ,
" Position" , $pos , "\n" ;
$n = 128;
$pos = findPosition( $n );
if (( $pos == -1) == true)
echo "n = " , $n , ", " ,
" Invalid number" , "\n" ;
else
echo "n = " , $n , ", " ,
" Position " , $pos , "\n" ;
?>
|
Output :
n = 0, Invalid number
n = 12, Invalid number
n = 128, Position 8
This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.