Find position of the only set bit - GeeksforGeeks

21 Jun, 2021
Given a number N having only one ‘1’ and all other ’0’s in its binary representation, find position of the only set bit. If there are 0 or more than 1 set bit the answer should be -1. Position of set bit ‘1’ should be counted starting with 1 from the LSB side in the binary representation of the number.
 Source: Microsoft Interview | 18
Examples:-
Input:
N = 2
Output:
2
Explanation:
2 is represented as "10" in Binary.
As we see there's only one set bit
and it's in Position 2 and thus the
Output 2.

here is another example
Input:
N = 5
Output:
-1
Explanation:
5 is represented as "101" in Binary.
As we see there's two set bits
and thus the Output -1.
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++
C
Java
Python3
C#
PHP
Javascript
// C++ program to find position of only set bit in a given number#include <bits/stdc++.h>using namespace std; // A utility function to check whether n is a power of 2 or not.// See http://goo.gl/17Arjint isPowerOfTwo(unsigned n){    return n && (!(n & (n - 1)));} // Returns position of the only set bit in 'n'int findPosition(unsigned n){    if (!isPowerOfTwo(n))        return -1;     unsigned i = 1, pos = 1;     // Iterate through bits of n till we find a set bit    // i&n will be non-zero only when 'i' and 'n' have a set bit    // at same position    while (!(i & n)) {        // Unset current bit and set the next bit in 'i'        i = i << 1;         // increment position        ++pos;    }     return pos;} // Driver program to test above functionint 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;} // This code is contributed by rathbhupendra
// C program to find position of only set bit in a given number#include <stdio.h> // A utility function to check whether n is a power of 2 or not.// See http://goo.gl/17Arjint isPowerOfTwo(unsigned n){    return n && (!(n & (n - 1)));} // Returns position of the only set bit in 'n'int findPosition(unsigned n){    if (!isPowerOfTwo(n))        return -1;     unsigned i = 1, pos = 1;     // Iterate through bits of n till we find a set bit    // i&n will be non-zero only when 'i' and 'n' have a set bit    // at same position    while (!(i & n)) {        // Unset current bit and set the next bit in 'i'        i = i << 1;         // increment position        ++pos;    }     return pos;} // Driver program to test above functionint 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 program to find position of only set bit in a given numberclass GFG {     // A utility function to check whether n is a power of 2 or not.    // See http://goo.gl/17Arj    static boolean isPowerOfTwo(int n)    {        return (n > 0 && ((n & (n - 1)) == 0)) ? true : false;    }     // Returns position of the only set bit in 'n'    static int findPosition(int n)    {        if (!isPowerOfTwo(n))            return -1;         int i = 1, pos = 1;         // Iterate through bits of n till we find a set bit        // i&n will be non-zero only when 'i' and 'n' have a set bit        // at same position        while ((i & n) == 0) {            // Unset current bit and set the next bit in 'i'            i = i << 1;             // increment position            ++pos;        }         return pos;    }     // Driver code    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);    }} // This code is contributed by mits
# Python3 program to find position of# only set bit in a given number # A utility function to check# whether n is power of 2 or# not.def isPowerOfTwo(n):    return (True if(n > 0 and                   ((n & (n - 1)) > 0))                 else False);     # Returns position of the# only set bit in 'n'def findPosition(n):    if (isPowerOfTwo(n) == True):        return -1;     i = 1;    pos = 1;     # Iterate through bits of n    # till we find a set bit i&n    # will be non-zero only when    # 'i' and 'n' have a set bit    # at same position    while ((i & n) == 0):                 # Unset current bit and        # set the next bit in 'i'        i = i << 1;         # increment position        pos += 1;     return pos; # Driver Coden = 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); # This code is contributed by mits
// C# program to find position of only set bit in a given numberusing System; class GFG {     // A utility function to check whether n is a power of 2 or not.    // See http://goo.gl/17Arj    static bool isPowerOfTwo(int n)    {        return (n > 0 && ((n & (n - 1)) == 0)) ? true : false;    }     // Returns position of the only set bit in 'n'    static int findPosition(int n)    {        if (!isPowerOfTwo(n))            return -1;         int i = 1, pos = 1;         // Iterate through bits of n till we find a set bit        // i&n will be non-zero only when 'i' and 'n' have a set bit        // at same position        while ((i & n) == 0) {            // Unset current bit and set the next bit in 'i'            i = i << 1;             // increment position            ++pos;        }         return pos;    }     // Driver code    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);    }} // This code is contributed by mits
<?php// PHP program to find position of// only set bit in a given number // A utility function to check// whether n is power of 2 or// not. See http://goo.gl/17Arjfunction isPowerOfTwo($n){    return $n && (!($n & ($n - 1)));     } // Returns position of the// only set bit in 'n'function findPosition($n){    if (!isPowerOfTwo($n))        return -1;     $i = 1;    $pos = 1;     // Iterate through bits of n    // till we find a set bit i&n    // will be non-zero only when    // 'i' and 'n' have a set bit    // at same position    while (!($i & $n))    {        // Unset current bit and        // set the next bit in 'i'        $i = $i << 1;         // increment position        ++$pos;    }     return $pos;} // Driver Code$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"; // This code is contributed by ajit?>
<script> // JavaScript program to find position of// only set bit in a given number // A utility function to check// whether n is power of 2 or// not.function isPowerOfTwo(n){         return (n > 0 && ((n & (n - 1)) == 0)) ? true : false;}     // Returns position of the// only set bit in 'n'function findPosition(n){    if (isPowerOfTwo(n) == false)        return -1;     var i = 1;    var pos = 1;     // Iterate through bits of n    // till we find a set bit i&n    // will be non-zero only when    // 'i' and 'n' have a set bit    // at same position    while ((i & n) == 0){                 // Unset current bit and        // set the next bit in 'i'        i = i << 1;         // increment position        pos += 1;    }    return pos;} // Driver Codevar n = 16;var pos = findPosition(n);if (pos == -1)    document.write("n =" + n + ", Invalid number");else    document.write("n =" + n + ", Position " + pos);    document.write("<br>"); n = 12;pos = findPosition(n);if (pos == -1)    document.write("n =" + n + ", Invalid number");else    document.write("n =" + n + ", Position ", pos);    document.write("<br>"); n = 128;pos = findPosition(n);if (pos == -1)    document.write("n =" + n + ", Invalid number");else    document.write("n =" + n + ", Position " + pos); // This code is contributed by AnkThon </script>
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++
C
Java
Python3
C#
PHP
Javascript
// C++ program to find position of only set bit in a given number#include <bits/stdc++.h>using namespace std; // A utility function to check whether n is power of 2 or notint isPowerOfTwo(unsigned n){    return n && (!(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;} // Driver codeint 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;} // This code is contributed by rathbhupendra
// C program to find position of only set bit in a given number#include <stdio.h> // A utility function to check whether n is power of 2 or notint isPowerOfTwo(unsigned n){    return n && (!(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;} // Driver program to test above functionint 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 program to find position of only// set bit in a given number class GFG {     // A utility function to check whether    // n is power of 2 or not    static boolean isPowerOfTwo(int n)    {        return n > 0 && ((n & (n - 1)) == 0);    }     // Returns position of the only set bit in 'n'    static int findPosition(int n)    {        if (!isPowerOfTwo(n))            return -1;         int count = 0;         // One by one move the only set bit        // to right till it reaches end        while (n > 0) {            n = n >> 1;             // increment count of shifts            ++count;        }         return count;    }     // Driver code    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);    }} // This code is contributed by mits
# Python 3 program to find position# of only set bit in a given number # A utility function to check whether# n is power of 2 or notdef isPowerOfTwo(n) :     return (n and ( not (n & (n-1)))) # Returns position of the only set bit in 'n'def findPosition(n) :     if not isPowerOfTwo(n) :        return -1     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 += 1     return count  # Driver program to test above functionif __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)    # This code is contributed by ANKITRAI1
// C# program to find position of only// set bit in a given numberusing System; class GFG {     // A utility function to check whether    // n is power of 2 or not    static bool isPowerOfTwo(int n)    {        return n > 0 && ((n & (n - 1)) == 0);    }     // Returns position of the only set bit in 'n'    static int findPosition(int n)    {        if (!isPowerOfTwo(n))            return -1;         int count = 0;         // One by one move the only set bit        // to right till it reaches end        while (n > 0) {            n = n >> 1;             // increment count of shifts            ++count;        }         return count;    }     // Driver code    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);    }} // This code is contributed by mits
<?php// PHP program to find position of// only set bit in a given number // A utility function to check// whether n is power of 2 or notfunction isPowerOfTwo($n){    return $n && (! ($n & ($n - 1)));} // Returns position of the// only set bit in 'n'function findPosition($n){    if (!isPowerOfTwo($n))        return -1;     $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;} // Driver Code$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";         // This code is contributed by ajit?>
<script> // JavaScript program to find position// of only set bit in a given number // A utility function to check whether// n is power of 2 or notfunction isPowerOfTwo(n) {     return (n && ( !(n & (n-1))))} // Returns position of the only set bit in 'n'function findPosition(n) {     if (!isPowerOfTwo(n))        return -1     var 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 += 1    }     return count } // Driver program to test above functionvar n = 0var pos = findPosition(n) if (pos == -1)    document.write("n = ", n, ", Invalid number ")else    document.write("n =", n, ", Position ", pos)     document.write("<br>") n = 12pos = findPosition(n) if (pos == -1)    document.write("n = ", n, ", Invalid number")else    document.write("n = ", n, ", Position ", pos) document.write("<br>") n = 128pos = findPosition(n) if (pos == -1)    document.write("n = ", n, ", Invalid number")else    document.write("n = ", n, ", Position ", pos)   document.write("<br>") // This code is contributed by AnkThon </script>
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++
C
Java
Python3
C#
PHP
Javascript
#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;} // Driver codeint 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;}  // This code is contributed by rathbhupendra
#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;} // Driver program to test above functionint 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 program to find position// of only set bit in a given number 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;    }     // Driver code    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);    }} // This code is contributed by mits
# Python program to find position# of only set bit in a given number def Log2n(n):    if (n > 1):        return (1 + Log2n(n / 2))    else:        return 0     # A utility function to check# whether n is power of 2 or not   def isPowerOfTwo(n):    return n and (not (n & (n-1)) )     def findPosition(n):    if (not isPowerOfTwo(n)):        return -1    return Log2n(n) + 1     # Driver program to test above function n = 0pos = findPosition(n)if(pos == -1):    print("n =", n, ", Invalid number")else:    print("n = ", n, ", Position ", pos)  n = 12pos = findPosition(n)if(pos == -1):    print("n =", n, ", Invalid number")else:    print("n = ", n, ", Position ", pos)n = 128pos = findPosition(n)if(pos == -1):    print("n = ", n, ", Invalid number")else:    print("n = ", n, ", Position ", pos)  # This code is contributed# by Sumit Sudhakar
// C# program to find position// of only set bit in a given number 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;    }     // Driver program to test above function    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);    }}// This code is contributed by mits
<?php// PHP program to find position// of only set bit in a given numberfunction 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;} // Driver Code$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"; // Driver Code$n = 128;$pos = findPosition($n);if(($pos == -1) == true)        echo "n = ", $n, ", ",             " Invalid number", "\n";else        echo "n = ", $n, ", ",             " Position ", $pos, "\n";         // This code is contributed by aj_36?>
<script> // JavaScript program to find position// of only set bit in a given number function Log2n(n){    if (n > 1)        return (1 + Log2n(n / 2))    else        return 0}     // A utility function to check// whether n is power of 2 or not   function isPowerOfTwo(n){    return n && ( ! (n & (n-1)) )}     function findPosition(n){    if (isPowerOfTwo(n) == false)        return -1    return Log2n(n) + 1 }     // Driver program to test above function var n = 0var pos = findPosition(n)if(pos == -1)    document.write("n = ", n, ", Invalid number")else    document.write("n = ", n, ", Position ", pos) document.write("<br>") n = 12pos = findPosition(n)if(pos == -1)    document.write("n = ", n, ", Invalid number")else    document.write("n = ", n, ", Position ", pos)  document.write("<br>")n = 128pos = findPosition(n)if(pos == -1)    document.write("n = ", n, ", Invalid number")else    document.write("n = ", n, ", Position ", pos)  // This code is contributed AnkThon  </script>
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. 
jit_t
Mithun Kumar
ankthon
rathbhupendra
CheshtaKwatra
surinderdawra388
jpafymzpbi
Microsoft
Bit Magic
Microsoft
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Add two numbers without using arithmetic operators
Binary representation of a given number
Program to find whether a given number is power of 2
Find the element that appears once
Set, Clear and Toggle a given bit of a number in C
Bit Fields in C
Bits manipulation (Important tactics)
Josephus problem | Set 1 (A O(n) Solution)