📜  计算要翻转以将A转换为B的位数

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

给定两个数字“ a”和“ b”。编写一个程序,计算将“ a”转换为“ b”所需翻转的位数。
例子 :

Input : a = 10, b = 20
Output : 4
Binary representation of a is 00001010
Binary representation of b is 00010100
We need to flip highlighted four bits in a
to make it b.

Input : a = 7, b = 10
Output : 3
Binary representation of a is 00000111
Binary representation of b is 00001010
We need to flip highlighted three bits in a
to make it b.
1. Calculate XOR of A and B.      
        a_xor_b = A ^ B
  2. Count the set bits in the above 
     calculated XOR result.
        countSetBits(a_xor_b)

只有在A与B不同的地方,两个数字的XOR才会设置位。

C++
// Count number of bits to be flipped
// to convert A into B
#include 
using namespace std;
 
// Function that count set bits
int countSetBits(int n)
{
    int count = 0;
    while (n > 0)
    {
        count++;
        n &= (n-1);
    }
    return count;
}
 
// Function that return count of
// flipped number
int FlippedCount(int a, int b)
{
    // Return count of set bits in
    // a XOR b
    return countSetBits(a^b);
}
 
// Driver code
int main()
{
    int a = 10;
    int b = 20;
    cout << FlippedCount(a, b)<


Java
// Count number of bits to be flipped
// to convert A into B
import java.util.*;
 
class Count {
     
    // Function that count set bits
    public static int countSetBits(int n)
    {
        int count = 0;
        while (n != 0) {
            count++;
            n &=(n-1);
        }
        return count;
    }
 
    // Function that return count of
    // flipped number
    public static int FlippedCount(int a, int b)
    {
        // Return count of set bits in
        // a XOR b
        return countSetBits(a ^ b);
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int a = 10;
        int b = 20;
        System.out.print(FlippedCount(a, b));
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Count number of bits to be flipped
# to convert A into B
 
# Function that count set bits
def countSetBits( n ):
    count = 0
    while n:
        count += 1
        n &= (n-1)
    return count
     
# Function that return count of
# flipped number
def FlippedCount(a , b):
 
    # Return count of set bits in
    # a XOR b
    return countSetBits(a^b)
 
# Driver code
a = 10
b = 20
print(FlippedCount(a, b))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// Count number of bits to be
// flipped to convert A into B
using System;
 
class Count {
     
    // Function that count set bits
    public static int countSetBits(int n)
    {
        int count = 0;
        while (n != 0) {
            count++;
            n &= (n-1);
        }
        return count;
    }
 
    // Function that return
    // count of flipped number
    public static int FlippedCount(int a, int b)
    {
    // Return count of set
    // bits in a XOR b
        return countSetBits(a ^ b);
    }
     
    // Driver code
    public static void Main()
    {
        int a = 10;
        int b = 20;
        Console.WriteLine(FlippedCount(a, b));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

4

感谢Sahil Rajput提供上述实施。