📜  两个数字中最右边的公共位的位置

📅  最后修改于: 2021-04-27 20:03:25             🧑  作者: Mango

给定两个非负数mn 。在数字的二进制表示中找到最右边相同位的位置。

例子:

Input : m = 10, n = 9
Output : 3
(10)10 = (1010)2
(9)10 = (1001)2
It can be seen that the 3rd bit
from the right is same.

Input : m = 16, n = 7
Output : 4
(16)10 = (10000)2
(7)10 = (111)2, can also be written as
     = (00111)2
It can be seen that the 4th bit
from the right is same.

方法:获得mn的按位异或。使其为xor_value = m ^ n。现在,获取xor_value中最右边未设置位的位置

说明:按位异或运算产生一个数字,该数字仅在mn的位相同的位置具有未设置的位。因此, xor_value中最右边的未设置位的位置给出了最右边相同位的位置。

C++
// C++ implementation to find the position
// of rightmost same bit
#include 
 
using namespace std;
 
// Function to find the position of
// rightmost set bit in 'n'
int getRightMostSetBit(unsigned int n)
{
    return log2(n & -n) + 1;
}
 
// Function to find the position of
// rightmost same bit in the
// binary representations of 'm' and 'n'
int posOfRightMostSameBit(unsigned int m,
                          unsigned int n)
{
    // position of rightmost same bit
    return getRightMostSetBit(~(m ^ n));
}
 
// Driver program to test above
int main()
{
    int m = 16, n = 7;
    cout << "Position = "
         << posOfRightMostSameBit(m, n);
    return 0;
}


Java
// Java implementation to find the position
// of rightmost same bit
class GFG {
         
    // Function to find the position of
    // rightmost set bit in 'n'
    static int getRightMostSetBit(int n)
    {
        return (int)((Math.log(n & -n))/(Math.log(2)))
                                                  + 1;
    }
     
    // Function to find the position of
    // rightmost same bit in the
    // binary representations of 'm' and 'n'
    static int posOfRightMostSameBit(int m,int n)
    {
         
        // position of rightmost same bit
        return getRightMostSetBit(~(m ^ n));
    }
     
    //Driver code
    public static void main (String[] args)
    {
        int m = 16, n = 7;
         
        System.out.print("Position = "
            + posOfRightMostSameBit(m, n));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 implementation to find the
# position of rightmost same bit
import math
 
# Function to find the position
# of rightmost set bit in 'n'
def getRightMostSetBit(n):
 
    return int(math.log2(n & -n)) + 1
 
# Function to find the position of
# rightmost same bit in the binary
# representations of 'm' and 'n'
def posOfRightMostSameBit(m, n):
 
    # position of rightmost same bit
    return getRightMostSetBit(~(m ^ n))
 
# Driver Code
m, n = 16, 7
print("Position = ", posOfRightMostSameBit(m, n))
 
# This code is contributed by Anant Agarwal.


C#
// C# implementation to find the position
// of rightmost same bit
using System;
 
class GFG
{
    // Function to find the position of
    // rightmost set bit in 'n'
    static int getRightMostSetBit(int n)
    {
        return (int)((Math.Log(n & -n)) / (Math.Log(2))) + 1;
    }
      
    // Function to find the position of
    // rightmost same bit in the
    // binary representations of 'm' and 'n'
    static int posOfRightMostSameBit(int m,int n)
    {
        // position of rightmost same bit
        return getRightMostSetBit(~(m ^ n));
    }
     
    //Driver code
    public static void Main ()
    {
        int m = 16, n = 7;
        Console.Write("Position = "
              + posOfRightMostSameBit(m, n));
    }
}
//This code is contributed by Anant Agarwal.


PHP


Javascript


C++
// C++ implementation to find the position
// of rightmost same bit
#include 
using namespace std;
     
// Function to find the position of
// rightmost same bit in the binary
// representations of 'm' and 'n'
static int posOfRightMostSameBit(int m, int n)
{
     
    // Initialize loop counter
    int loopCounter = 1;
     
    while (m > 0 || n > 0)
    {
         
        // Check whether the value 'm' is odd
        bool a = m % 2 == 1;
         
        // Check whether the value 'n' is odd
        bool b = n % 2 == 1;
         
        // Below 'if' checks for both
        // values to be odd or even
        if (!(a ^ b))
        {
            return loopCounter;
        }
         
        // Right shift value of m
        m = m >> 1;
         
        // Right shift value of n
        n = n >> 1;
        loopCounter++;
    }
     
    // When no common set is found
    return -1;
}
 
// Driver code
int main()
{
    int m = 16, n = 7;
     
    cout << "Position = "
         <<  posOfRightMostSameBit(m, n);
}
 
// This code is contributed by shivanisinghss2110


Java
// Java implementation to find the position
// of rightmost same bit
class GFG {
     
    // Function to find the position of
    // rightmost same bit in the
    // binary representations of 'm' and 'n'
    static int posOfRightMostSameBit(int m,int n)
    {
        int loopCounter = 1; // Initialize loop counter
        while (m > 0 || n > 0){
             
            boolean a = m%2 == 1; //Check whether the value 'm' is odd
            boolean b = n%2 == 1; //Check whether the value 'n' is odd
             
            // Below 'if' checks for both values to be odd or even
            if (!(a ^ b)){
                return loopCounter;}
             
            m = m >> 1; //Right shift value of m
            n = n >> 1; //Right shift value of n
            loopCounter++;
        }
        return -1; //When no common set is found
    }
       
    //Driver code
    public static void main (String[] args)
    {
        int m = 16, n = 7;
           
        System.out.print("Position = "
            + posOfRightMostSameBit(m, n));
    }
}


Python3
# Python3 implementation to find the position
# of rightmost same bit
 
# Function to find the position of
# rightmost same bit in the
# binary representations of 'm' and 'n'
def posOfRightMostSameBit(m, n):
     
    # Initialize loop counter
    loopCounter = 1
     
    while (m > 0 or n > 0):
         
        # Check whether the value 'm' is odd
        a = m % 2 == 1
         
        # Check whether the value 'n' is odd
        b = n % 2 == 1
 
        # Below 'if' checks for both
        # values to be odd or even
        if (not (a ^ b)):
            return loopCounter
             
        # Right shift value of m
        m = m >> 1
         
        # Right shift value of n
        n = n >> 1
        loopCounter += 1
         
    # When no common set is found
    return -1
 
# Driver code
if __name__ == '__main__':
     
    m, n = 16, 7
 
    print("Position = ",
    posOfRightMostSameBit(m, n))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation to find the position
// of rightmost same bit
using System;
class GFG
{
 
  // Function to find the position of
  // rightmost same bit in the
  // binary representations of 'm' and 'n'
  static int posOfRightMostSameBit(int m, int n)
  {
    int loopCounter = 1; // Initialize loop counter
    while (m > 0 || n > 0)
    {
 
      Boolean a = m % 2 == 1; // Check whether the value 'm' is odd
      Boolean b = n % 2 == 1; // Check whether the value 'n' is odd
 
      // Below 'if' checks for both values to be odd or even
      if (!(a ^ b))
      {
        return loopCounter;
      }
 
      m = m >> 1; // Right shift value of m
      n = n >> 1; // Right shift value of n
      loopCounter++;
    }
    return -1; // When no common set is found
  }
 
  // Driver code
  public static void Main (String[] args)
  {
    int m = 16, n = 7;        
    Console.Write("Position = "
                  + posOfRightMostSameBit(m, n));
  }
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:

Position = 4

替代方法:在两个值都变为零之前,检查两个数字的最后一位并右移。任何时候,两个位都是相同的,返回计数器。

说明:两个值mn的最右位仅在两个值均为奇数或偶数时才相等。

C++

// C++ implementation to find the position
// of rightmost same bit
#include 
using namespace std;
     
// Function to find the position of
// rightmost same bit in the binary
// representations of 'm' and 'n'
static int posOfRightMostSameBit(int m, int n)
{
     
    // Initialize loop counter
    int loopCounter = 1;
     
    while (m > 0 || n > 0)
    {
         
        // Check whether the value 'm' is odd
        bool a = m % 2 == 1;
         
        // Check whether the value 'n' is odd
        bool b = n % 2 == 1;
         
        // Below 'if' checks for both
        // values to be odd or even
        if (!(a ^ b))
        {
            return loopCounter;
        }
         
        // Right shift value of m
        m = m >> 1;
         
        // Right shift value of n
        n = n >> 1;
        loopCounter++;
    }
     
    // When no common set is found
    return -1;
}
 
// Driver code
int main()
{
    int m = 16, n = 7;
     
    cout << "Position = "
         <<  posOfRightMostSameBit(m, n);
}
 
// This code is contributed by shivanisinghss2110

Java

// Java implementation to find the position
// of rightmost same bit
class GFG {
     
    // Function to find the position of
    // rightmost same bit in the
    // binary representations of 'm' and 'n'
    static int posOfRightMostSameBit(int m,int n)
    {
        int loopCounter = 1; // Initialize loop counter
        while (m > 0 || n > 0){
             
            boolean a = m%2 == 1; //Check whether the value 'm' is odd
            boolean b = n%2 == 1; //Check whether the value 'n' is odd
             
            // Below 'if' checks for both values to be odd or even
            if (!(a ^ b)){
                return loopCounter;}
             
            m = m >> 1; //Right shift value of m
            n = n >> 1; //Right shift value of n
            loopCounter++;
        }
        return -1; //When no common set is found
    }
       
    //Driver code
    public static void main (String[] args)
    {
        int m = 16, n = 7;
           
        System.out.print("Position = "
            + posOfRightMostSameBit(m, n));
    }
}

Python3

# Python3 implementation to find the position
# of rightmost same bit
 
# Function to find the position of
# rightmost same bit in the
# binary representations of 'm' and 'n'
def posOfRightMostSameBit(m, n):
     
    # Initialize loop counter
    loopCounter = 1
     
    while (m > 0 or n > 0):
         
        # Check whether the value 'm' is odd
        a = m % 2 == 1
         
        # Check whether the value 'n' is odd
        b = n % 2 == 1
 
        # Below 'if' checks for both
        # values to be odd or even
        if (not (a ^ b)):
            return loopCounter
             
        # Right shift value of m
        m = m >> 1
         
        # Right shift value of n
        n = n >> 1
        loopCounter += 1
         
    # When no common set is found
    return -1
 
# Driver code
if __name__ == '__main__':
     
    m, n = 16, 7
 
    print("Position = ",
    posOfRightMostSameBit(m, n))
 
# This code is contributed by mohit kumar 29

C#

// C# implementation to find the position
// of rightmost same bit
using System;
class GFG
{
 
  // Function to find the position of
  // rightmost same bit in the
  // binary representations of 'm' and 'n'
  static int posOfRightMostSameBit(int m, int n)
  {
    int loopCounter = 1; // Initialize loop counter
    while (m > 0 || n > 0)
    {
 
      Boolean a = m % 2 == 1; // Check whether the value 'm' is odd
      Boolean b = n % 2 == 1; // Check whether the value 'n' is odd
 
      // Below 'if' checks for both values to be odd or even
      if (!(a ^ b))
      {
        return loopCounter;
      }
 
      m = m >> 1; // Right shift value of m
      n = n >> 1; // Right shift value of n
      loopCounter++;
    }
    return -1; // When no common set is found
  }
 
  // Driver code
  public static void Main (String[] args)
  {
    int m = 16, n = 7;        
    Console.Write("Position = "
                  + posOfRightMostSameBit(m, n));
  }
}
 
// This code is contributed by shivanisinghss2110

Java脚本


输出:

Position = 4