📜  最右边不同位的位置

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

给定两个数字mn 。在数字的二进制表示中找到最右边的不同位的位置。可以保证存在这样的一点。

例子:

Input: m = 11, n = 9
Output: 2
(11)10 = (1011)2
(9)10 = (1001)2
It can be seen that 2nd bit from
the right is different 

Input: m = 52, n = 4
Output: 5
(52)10 = (110100)2
(4)10 = (100)2, can also be written as
     = (000100)2
It can be seen that 5th bit from
the right is different

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

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

下面是上述方法的实现:

C++
// C++ implementation to find the position
// of rightmost different bit
#include 
using namespace std;
 
// Function to find the position of
// rightmost set bit in 'n'
// returns 0 if there is no set bit.
int getRightMostSetBit(int n)
{
    // to handle edge case when n = 0. 
    if (n == 0) 
        return 0;
     
    return log2(n & -n) + 1;
}
 
// Function to find the position of
// rightmost different bit in the
// binary representations of 'm' and 'n'
// returns 0 if there is no
// rightmost diffrent bit.
int posOfRightMostDiffBit(int m, int n)
{
    // position of rightmost different
    //  bit
     
    return getRightMostSetBit(m ^ n);
}
 
// Driver program
int main()
{
    int m = 52, n = 24;
   
    cout << "Position of rightmost diffrent bit:"
         << posOfRightMostDiffBit(m, n)<


Java
// Java implementation to find the position
// of rightmost different bit
 
class GFG {
     
    // Function to find the position of
    // rightmost set bit in 'n'
    // return 0 if there is no set bit.
    static int getRightMostSetBit(int n)
    {
        if(n == 0)
          return 0;
       
        return (int)((Math.log10(n & -n)) /
                     Math.log10(2)) + 1;
    }
     
    // Function to find the position of
    // rightmost different bit in the
    // binary representations of 'm' and 'n'
    static int posOfRightMostDiffBit(int m, int n)
    {
        // position of rightmost different bit
        return getRightMostSetBit(m ^ n);
    }
     
    // Driver code
    public static void main(String arg[])
    {
        int m = 52, n = 4;
        System.out.print("Position = " +
            posOfRightMostDiffBit(m, n));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python implementation
# to find the position
# of rightmost different bit
 
import math
 
# Function to find the position of
# rightmost set bit in 'n'
def getRightMostSetBit(n):
    if (n == 0):
        return 0
 
    return math.log2(n & -n) + 1
 
 
# Function to find the position of
# rightmost different bit in the
# binary representations of 'm' and 'n'
def posOfRightMostDiffBit(m, n):
 
    # position of rightmost different
    # bit
    return getRightMostSetBit(m ^ n)
 
# Driver code
 
m = 52
n = 4
print("position = ", int(posOfRightMostDiffBit(m, n)))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# implementation to find the position
// of rightmost different bit
using System;
 
class GFG {
     
    // Function to find the position of
    // rightmost set bit in 'n'
    static int getRightMostSetBit(int n)
    {
        if (n == 0)
            return 0;
        return (int)((Math.Log10(n & -n))
                       / Math.Log10(2)) + 1;
    }
     
    // Function to find the position of
    // rightmost different bit in the
    // binary representations of 'm' and 'n'
    static int posOfRightMostDiffBit(int m, int n)
    {
        // position of rightmost different bit
        return getRightMostSetBit(m ^ n);
    }
     
    // Driver code
    public static void Main()
    {
        int m = 52, n = 4;
        Console.Write("Position = " +
            posOfRightMostDiffBit(m, n));
    }
}
 
// This code is contributed by Smitha.


PHP


Javascript


C++
// C++ implementation to find the
// position of rightmost different
// bit in two number.
#include 
using namespace std;
  
// function to find rightmost different
//  bit in two numbers.
int posOfRightMostDiffBit(int m, int n)
{
    return ffs(m ^ n);
}
  
// Driver code
int main()
{
    int m = 52, n = 4;
    cout <<"Position = " <<
         posOfRightMostDiffBit(m, n);
    return 0;
}


Java
// Java implementation to find the
// position of rightmost different
// bit in two number.
import java.util.*;
class GFG{
  
// function to find rightmost
// different bit in two numbers.
static int posOfRightMostDiffBit(int m,
                                 int n)
{
  return (int)Math.floor(
              Math.log10(
              Math.pow(m ^ n,
                       2)))+2;
}
  
// Driver code
public static void main(String[] args)
{
  int m = 52, n = 4;
  System.out.println("Position = " + 
                     posOfRightMostDiffBit(m, n));
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 implementation to find the
# position of rightmost different
# bit in two number.
from math import floor, log10
 
# Function to find rightmost different
# bit in two numbers.
def posOfRightMostDiffBit(m, n):
     
    return floor(log10(pow(m ^ n, 2))) + 2
 
# Driver code
if __name__ == '__main__':
     
    m, n = 52, 4
     
    print("Position = ",
    posOfRightMostDiffBit(m, n))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation to find the
// position of rightmost different
// bit in two number.
using System;
class GFG
{
 
  // function to find rightmost
  // different bit in two numbers.
  static int posOfRightMostDiffBit(int m,
                                   int n)
  {
    return (int)Math.Floor(Math.Log10(
      Math.Pow(m ^ n, 2))) + 2;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int m = 52, n = 4;
    Console.Write("Position = " +
                  posOfRightMostDiffBit(m, n));
  }
}
 
// This code is contributed by shivanisinghss2110


PHP


输出
Position of rightmost diffrent bit:3

使用ffs()函数

C++

// C++ implementation to find the
// position of rightmost different
// bit in two number.
#include 
using namespace std;
  
// function to find rightmost different
//  bit in two numbers.
int posOfRightMostDiffBit(int m, int n)
{
    return ffs(m ^ n);
}
  
// Driver code
int main()
{
    int m = 52, n = 4;
    cout <<"Position = " <<
         posOfRightMostDiffBit(m, n);
    return 0;
}

Java

// Java implementation to find the
// position of rightmost different
// bit in two number.
import java.util.*;
class GFG{
  
// function to find rightmost
// different bit in two numbers.
static int posOfRightMostDiffBit(int m,
                                 int n)
{
  return (int)Math.floor(
              Math.log10(
              Math.pow(m ^ n,
                       2)))+2;
}
  
// Driver code
public static void main(String[] args)
{
  int m = 52, n = 4;
  System.out.println("Position = " + 
                     posOfRightMostDiffBit(m, n));
}
}
 
// This code is contributed by gauravrajput1

Python3

# Python3 implementation to find the
# position of rightmost different
# bit in two number.
from math import floor, log10
 
# Function to find rightmost different
# bit in two numbers.
def posOfRightMostDiffBit(m, n):
     
    return floor(log10(pow(m ^ n, 2))) + 2
 
# Driver code
if __name__ == '__main__':
     
    m, n = 52, 4
     
    print("Position = ",
    posOfRightMostDiffBit(m, n))
 
# This code is contributed by mohit kumar 29

C#

// C# implementation to find the
// position of rightmost different
// bit in two number.
using System;
class GFG
{
 
  // function to find rightmost
  // different bit in two numbers.
  static int posOfRightMostDiffBit(int m,
                                   int n)
  {
    return (int)Math.Floor(Math.Log10(
      Math.Pow(m ^ n, 2))) + 2;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int m = 52, n = 4;
    Console.Write("Position = " +
                  posOfRightMostDiffBit(m, n));
  }
}
 
// This code is contributed by shivanisinghss2110

的PHP


输出
Position = 5