📜  获取最右边未设置位的位置

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

给定一个非负数n 。在n的二进制表示中找到最右边的未设置位的位置,请考虑位置1的最后一位,位置2的最后一位,依此类推。如果n的二进制表示中不存在0 ,则为0 。然后打印“ -1”。
例子:

Input : n = 9
Output : 2
(9)10 = (1001)2
The position of rightmost unset bit in the binary
representation of 9 is 2.

Input : n = 32
Output : 1

方法:以下是步骤:

  1. 如果n = 0,则返回1。
  2. 如果n的所有位都置1,则返回-1。请参阅这篇文章。
  3. 否则,不按给定数字执行按位运算(等于1的补码的运算)。使其为num =〜n。
  4. 获取num的最右边设置位的位置。这将是n的最右边未设置位的位置。
C++
// C++ implementation to get the position of rightmost unset bit
#include 
 
using namespace std;
 
// function to find the position
// of rightmost set bit
int getPosOfRightmostSetBit(int n)
{
    return log2(n&-n)+1;
}
 
// function to get the position of rightmost unset bit
int getPosOfRightMostUnsetBit(int n)
{
    // if n = 0, return 1
    if (n == 0)
        return 1;
     
    // if all bits of 'n' are set
    if ((n & (n + 1)) == 0)   
        return -1;
     
    // position of rightmost unset bit in 'n'
    // passing ~n as argument
    return getPosOfRightmostSetBit(~n);       
}
 
// Driver program to test above
int main()
{
    int n = 9;
    cout << getPosOfRightMostUnsetBit(n);
    return 0;
}


Java
// Java implementation to get the
// position of rightmost unset bit
 
class GFG {
     
// function to find the position
// of rightmost set bit
static int getPosOfRightmostSetBit(int n)
{
    return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1;
}
 
// function to get the position
// of rightmost unset bit
static int getPosOfRightMostUnsetBit(int n) {
     
    // if n = 0, return 1
    if (n == 0)
    return 1;
 
    // if all bits of 'n' are set
    if ((n & (n + 1)) == 0)
    return -1;
 
    // position of rightmost unset bit in 'n'
    // passing ~n as argument
    return getPosOfRightmostSetBit(~n);
}
 
// Driver code
public static void main(String arg[])
{
    int n = 9;
    System.out.print(getPosOfRightMostUnsetBit(n));
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 implementation to get the position
# of rightmost unset bit
 
# import library
import math as m
  
# function to find the position
# of rightmost set bit
def getPosOfRightmostSetBit(n):
     
    return (m.log(((n & - n) + 1),2))
 
  
# function to get the position ot rightmost unset bit
def getPosOfRightMostUnsetBit(n):
     
    # if n = 0, return 1
    if (n == 0):
        return 1
      
    # if all bits of 'n' are set
    if ((n & (n + 1)) == 0):
        return -1
      
    # position of rightmost unset bit in 'n'
    # passing ~n as argument
    return getPosOfRightmostSetBit(~n)   
 
  
# Driver program to test above
n = 13;
ans = getPosOfRightMostUnsetBit(n)
 
#rounding the final answer
print (round(ans))
 
# This code is contributed by Saloni Gupta.


C#
// C# implementation to get the
// position of rightmost unset bit
using System;
 
class GFG
{
    // function to find the position
    // of rightmost set bit
    static int getPosOfRightmostSetBit(int n)
    {
        return (int)((Math.Log10(n & -n)) / Math.Log10(2)) + 1;
    }
     
    // function to get the position
    // of rightmost unset bit
    static int getPosOfRightMostUnsetBit(int n) {
         
        // if n = 0, return 1
        if (n == 0)
        return 1;
     
        // if all bits of 'n' are set
        if ((n & (n + 1)) == 0)
        return -1;
     
        // position of rightmost unset bit in 'n'
        // passing ~n as argument
        return getPosOfRightmostSetBit(~n);
    }
     
    // Driver code
    public static void Main()
    {
        int n = 9;
        Console.Write(getPosOfRightMostUnsetBit(n));
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出:

2