📌  相关文章
📜  查找两个数字最不相似的位的位置

📅  最后修改于: 2021-05-25 00:47:42             🧑  作者: Mango

给定两个数字n1n2 。任务是从左开始查找两个数字的二进制表示形式中的第一个不匹配位的位置。我们需要在使两个数字的二进制表示形式的长度相同之后找到该位。我们通过在较小的数字后附加0来使长度相同。

注意事项

  • 例如:n1 = 1,n2 =7。n1和n4的按位表示分别为“ 1”和“ 111”。将两个零附加到n1以使其为100。
  • 如果n1等于n2,则输出零。

例子

Input: n1 = 12, n2 = 34
Output: 2
Binary representation of 12 is 1100 and of 34 is 100010. 
First make both representations of the 
same length by appending 0s. 
So the first representation now becomes 11000. 
The second bit is the different bit.

Input: n1 = 1, n2 = 2
Output: 2

为了在两个数字的比特表示中找到最不相似的比特的位置,可以进行逐比特比较或者可以使用导出的公式。尽管两者的时间复杂度相同。
为了首先找到最不相似的位,请先将两个数字的位长乘以pow(2,位长差)来使两个数的位长相等。使位长相等后,取两个数字的XOR。现在,最左端的非相似位清楚地反映在XOR值中。从给定数加1的位长中减去XOR值的位长,可以得出最左异位的位置。

算法:

  1. 查找n1和n2的位长。
  2. 通过将零放在小数的右边来使两个数的位长相等(与将小数乘以pow(2,位长差)相同)
  3. 取两个数的异或
  4. 任意数字的位长与XOR值的位长之差要求答案加1

下面是上述方法的实现:

C++
// C++ program to find the leftmost
// position of first dis-similar bit
#include 
using namespace std;
 
// Function to find first dis-similar bit
int bitPos(int n1, int n2)
{
    // return zero for equal number
    if (n1 == n2)
        return 0;
 
    /** find the 1st dis-similar bit **/
    // count bit length of n1 and n2
    int bitCount1 = floor(log2(n1)) + 1;
    int bitCount2 = floor(log2(n2)) + 1;
 
    // find bit difference and maxBit
    int bitDiff = abs(bitCount1 - bitCount2);
    int maxBitCount = max(bitCount1, bitCount2);
 
    if (bitCount1 > bitCount2) {
        n2 = n2 * pow(2, bitDiff);
    }
    else {
        n1 = n1 * pow(2, bitDiff);
    }
 
    int xorValue = n1 ^ n2;
    int bitCountXorValue = floor(log2(xorValue)) + 1;
    int disSimilarBitPosition = maxBitCount -
                                  bitCountXorValue + 1;
 
    return disSimilarBitPosition;
}
 
// Driver program
int main()
{
    int n1 = 53, n2 = 55;
    cout << bitPos(n1, n2);
    return 0;
}


Java
// Java program to find the leftmost position of
// first dis-similar bit
 
import java.io.*;
 
class GFG {
         
// Function to find first dis-similar bit
static int bitPos(int n1, int n2)
{
    // return zero for equal number
    if (n1 == n2)
        return 0;
 
    /** find the 1st dis-similar bit **/
    // count bit length of n1 and n2
    int bitCount1 = (int)Math.floor(Math.log(n1) /
                                    Math.log(2)) + 1;
    int bitCount2 = (int)Math.floor(Math.log(n2) /
                                    Math.log(2)) + 1;
 
    // find bit difference and maxBit
    int bitDiff = Math.abs(bitCount1 - bitCount2);
    int maxBitCount = Math.max(bitCount1,
                            bitCount2);
 
    if (bitCount1 > bitCount2)
    {
        n2 = n2 * (int)Math.pow(2, bitDiff);
    }
    else
    {
        n1 = n1 * (int)Math.pow(2, bitDiff);
    }
 
    int xorValue = n1 ^ n2;
    int bitCountXorValue = (int)Math.floor(Math.log(xorValue) /
                                        Math.log(2)) + 1;
    int disSimilarBitPosition = maxBitCount -
                                bitCountXorValue + 1;
 
    return disSimilarBitPosition;
}
 
// Driver Code
    public static void main (String[] args) {
 
        int n1 = 53, n2 = 55;
        System.out.println(bitPos(n1, n2));
}
}
// This code is contributed by ajit


Python3
# Python3 program to Find the leftmost
# position of first dis-similar bit
 
# from math lib import floor()
# and log2()
from math import floor, log2
 
# Function to find first
# dis-similar bit
def bitPos(n1, n2) :
     
    # return zero for equal number
    if n1 == n2 :
        return 0
     
    # find the 1st dis-similar bit
    # count bit length of n1 and n
    bitCount1 = floor(log2(n1)) + 1
    bitCount2 = floor(log2(n2)) + 1
     
    # find bit difference and maxBit
    bitDiff = abs(bitCount1 - bitCount2)
    maxBitCount = max(bitCount1, bitCount2)
     
    if (bitCount1 > bitCount2) :
         
        n2 *= pow(2, bitDiff)
     
    else :
         
        n1 *= pow(2, bitDiff)
         
    xorValue = n1 ^ n2
    bitCountXorValue = floor(log2(xorValue)) + 1
    disSimilarBitPosition = (maxBitCount -
                             bitCountXorValue + 1)
     
    return disSimilarBitPosition
     
# Driver code
if __name__ == "__main__" :
     
    n1, n2 = 53, 55
    print(bitPos(n1, n2))
     
# This code is contributed by Ryuga


C#
// C# to find the leftmost position of
// first dis-similar bit
using System;
 
class GFG
{
     
// Function to find first dis-similar bit
static int bitPos(int n1, int n2)
{
    // return zero for equal number
    if (n1 == n2)
        return 0;
 
    /** find the 1st dis-similar bit **/
    // count bit length of n1 and n2
    int bitCount1 = (int)Math.Floor(Math.Log(n1) /
                                    Math.Log(2)) + 1;
    int bitCount2 = (int)Math.Floor(Math.Log(n2) /
                                    Math.Log(2)) + 1;
 
    // find bit difference and maxBit
    int bitDiff = Math.Abs(bitCount1 - bitCount2);
    int maxBitCount = Math.Max(bitCount1,
                               bitCount2);
 
    if (bitCount1 > bitCount2)
    {
        n2 = n2 * (int)Math.Pow(2, bitDiff);
    }
    else
    {
        n1 = n1 * (int)Math.Pow(2, bitDiff);
    }
 
    int xorValue = n1 ^ n2;
    int bitCountXorValue = (int)Math.Floor(Math.Log(xorValue) /
                                           Math.Log(2)) + 1;
    int disSimilarBitPosition = maxBitCount -
                                bitCountXorValue + 1;
 
    return disSimilarBitPosition;
}
 
// Driver Code
public static void Main()
{
    int n1 = 53, n2 = 55;
    Console.Write(bitPos(n1, n2));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP
 $bitCount2)
    {
        $n2 = $n2 * pow(2, $bitDiff);
    }
    else {
        $n1 = $n1 * pow(2, $bitDiff);
    }
 
    $xorValue = $n1 ^ $n2;
    $bitCountXorValue = floor(log($xorValue, 2)) + 1;
    $disSimilarBitPosition = $maxBitCount -
                             $bitCountXorValue + 1;
 
    return $disSimilarBitPosition;
}
 
// Driver Code
$n1 = 53;
$n2 = 55;
echo bitPos($n1, $n2);
     
// This code is contributed by ajit
?>


Javascript


输出:
5