📜  查找唯一置位的位置

📅  最后修改于: 2021-04-29 09:11:50             🧑  作者: Mango

给定一个在二进制表示中只有一个“ 1”和所有其他“ 0”的数字,请找到唯一置位的位置。资料来源:Microsoft专访| 18岁

这个想法是从最右边的位开始,然后从每一位开始一个一个的校验值。以下是详细的算法。
1)如果number是2的幂,则仅其二进制表示形式仅包含一个“ 1”。这就是为什么检查给定数字是否为2的幂。如果给定的数字不是2的幂,则打印错误消息并退出。
2)初始化两个变量; i = 1(用于循环)和pos = 1(用于找到设置位的位置)
3)在循环内,对i和数字“ N”进行按位与运算。如果该操作的值为true,则将“ pos”位置1,从而中断循环并返回位置。否则,将“ pos”增加1,将i左移1,然后重复该过程。

C++
// C++ program to find position of only set bit in a given number
#include 
using namespace std;
 
// A utility function to check whether n is a power of 2 or not.
// See http://goo.gl/17Arj
int 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 function
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;
}
 
// This code is contributed by rathbhupendra


C
// C program to find position of only set bit in a given number
#include 
 
// A utility function to check whether n is a power of 2 or not.
// See http://goo.gl/17Arj
int 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 function
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
// Java program to find position of only set bit in a given number
class 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
# 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 Code
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);
 
# This code is contributed by mits


C#
// C# program to find position of only set bit in a given number
using 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


Javascript


C++
// C++ program to find position of only set bit in a given number
#include 
using namespace std;
 
// A utility function to check whether n is power of 2 or not
int 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 code
int main(void)
{
    int n = 0;
    int pos = findPosition(n);
    (pos == -1) ? cout<<"n = "<


C
// C program to find position of only set bit in a given number
#include 
 
// A utility function to check whether n is power of 2 or not
int 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 function
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
// 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 cotributed by mits


Python3
# 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 not
def 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 function
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)
    
# This code is contributed by ANKITRAI1


C#
// C# program to find position of only
// set bit in a given number
using 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 cotributed by mits


PHP
> 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
?>


Javascript


C++
#include 
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 code
int main(void)
{
    int n = 0;
    int pos = findPosition(n);
    (pos == -1) ? cout<<"n = "<


C
#include 
 
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 function
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
// 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


Python3
# 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 = 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 Sumit Sudhakar


C#
// 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
 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
?>


Javascript


输出 :

n = 16, Position 5
n = 12, Invalid number
n = 128, Position 8

以下是解决此问题的另一种方法。想法是将给定数字’n’的设置位一位一位地右移,直到’n’变为0。计算我们移位多少次以使’n’为零。最终计数是设置位的位置。

C++

// C++ program to find position of only set bit in a given number
#include 
using namespace std;
 
// A utility function to check whether n is power of 2 or not
int 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 code
int main(void)
{
    int n = 0;
    int pos = findPosition(n);
    (pos == -1) ? cout<<"n = "<

C

// C program to find position of only set bit in a given number
#include 
 
// A utility function to check whether n is power of 2 or not
int 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 function
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

// 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 cotributed by mits

Python3

# 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 not
def 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 function
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)
    
# This code is contributed by ANKITRAI1

C#

// C# program to find position of only
// set bit in a given number
using 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 cotributed by mits

的PHP

> 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
?>

Java脚本


输出 :

n = 0, Invalid number
n = 12, Invalid number
n = 128, Position 8

我们还可以使用对数基数2来找到位置。感谢Arunkumar提出此解决方案。

C++

#include 
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 code
int main(void)
{
    int n = 0;
    int pos = findPosition(n);
    (pos == -1) ? cout<<"n = "<

C

#include 
 
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 function
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

// 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

Python3

# 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 = 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 Sumit Sudhakar

C#

// 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

 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
?>

Java脚本


输出 :

n = 0, Invalid number
n = 12, Invalid number
n = 128, Position 8