📌  相关文章
📜  两个整数的二进制表示形式中的不匹配位数

📅  最后修改于: 2021-05-25 02:20:36             🧑  作者: Mango

给定两个整数(小于2 ^ 31),A和B。任务是查找二进制表示形式不同的位数。

例子:

Input :  A = 12, B = 15
Output : Number of different bits : 2
Explanation: The binary representation of 
12 is 1100 and 15 is 1111.
So, the number of different bits are 2.

Input : A = 3, B = 16
Output : Number of different bits : 3

方法:

  • 从“ 0”到“ 31”运行一个循环,然后将A和B的位右移“ i”位,然后检查“ 0th”位置的位是否不同。
  • 如果位不同,则增加计数。
  • 由于数字小于2 ^ 31,因此我们只需要运行循环“ 32”次,即从“ 0”到“ 31”。
  • 如果我们将数字按1与1相加,就可以得到第一位。
  • 在循环末尾显示计数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// compute number of different bits
void solve(int A, int B)
{
    int count = 0;
 
    // since, the numbers are less than 2^31
    // run the loop from '0' to '31' only
    for (int i = 0; i < 32; i++) {
 
        // right shift both the numbers by 'i' and
        // check if the bit at the 0th position is different
        if (((A >> i) & 1) != ((B >> i) & 1)) {
            count++;
        }
    }
 
    cout << "Number of different bits : " << count << endl;
}
 
// Driver code
int main()
{
    int A = 12, B = 15;
 
    // find number of different bits
    solve(A, B);
 
    return 0;
}


Java
// Java implementation of the approach
 
import java.io.*;
 
class GFG {
 
// compute number of different bits
static void solve(int A, int B)
{
    int count = 0;
 
    // since, the numbers are less than 2^31
    // run the loop from '0' to '31' only
    for (int i = 0; i < 32; i++) {
 
        // right shift both the numbers by 'i' and
        // check if the bit at the 0th position is different
        if (((A >> i) & 1) != ((B >> i) & 1)) {
            count++;
        }
    }
 
    System.out.println("Number of different bits : " + count);
}
 
// Driver code
 
 
    public static void main (String[] args) {
        int A = 12, B = 15;
 
    // find number of different bits
    solve(A, B);
 
    }
}
// this code is contributed by anuj_67..


Python3
# Python3 implementation of the approach
 
# compute number of different bits
def solve( A,  B):
  
    count = 0
 
    # since, the numbers are less than 2^31
    # run the loop from '0' to '31' only
    for i in range(0,32):
 
        # right shift both the numbers by 'i' and
        # check if the bit at the 0th position is different
        if ((( A >>  i) & 1) != (( B >>  i) & 1)):
             count=count+1
          
      
 
    print("Number of different bits :",count)
  
 
# Driver code
A = 12
B = 15
 
# find number of different bits
solve( A,  B)
 
 
# This code is contributed by ihritik


C#
// C# implementation of the approach
 
using System;
class GFG
{
    // compute number of different bits
    static void solve(int A, int B)
    {
        int count = 0;
     
        // since, the numbers are less than 2^31
        // run the loop from '0' to '31' only
        for (int i = 0; i < 32; i++) {
     
            // right shift both the numbers by 'i' and
            // check if the bit at the 0th position is different
            if (((A >> i) & 1) != ((B >> i) & 1)) {
                count++;
            }
        }
     
        Console.WriteLine("Number of different bits : " + count);
    }
     
    // Driver code
    public static void  Main()
    {
        int A = 12, B = 15;
     
        // find number of different bits
        solve(A, B);
     
    }
 
}
 
// This code is contributed by ihritik


PHP
> $i) & 1) != (($B >> $i) & 1)) {
            $count++;
        }
    }
 
    echo "Number of different bits : $count";
}
 
// Driver code
$A = 12;
$B = 15;
 
// find number of different bits
solve($A, $B);
 
// This code is contributed by ihritik
?>


Javascript


输出:
Number of different bits : 2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。