📜  计算要翻转以将A转换为B的位数|套装2

📅  最后修改于: 2021-05-25 08:35:41             🧑  作者: Mango

给定两个整数AB ,任务是计算将A转换为B所需翻转的位数。
例子:

方法:这里已经讨论了解决此问题的方法。在这里,可以通过将两个整数中的所有位一一匹配来找到需要翻转的位的数量。如果所考虑的位不同,则增加计数。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of bits
// to be flipped to convert a to b
int countBits(int a, int b)
{
 
    // To store the required count
    int count = 0;
 
    // Loop until both of them become zero
    while (a || b) {
 
        // Store the last bits in a
        // as well as b
        int last_bit_a = a & 1;
        int last_bit_b = b & 1;
 
        // If the current bit is not same
        // in both the integers
        if (last_bit_a != last_bit_b)
            count++;
 
        // Right shift both the integers by 1
        a = a >> 1;
        b = b >> 1;
    }
 
    // Return the count
    return count;
}
 
// Driver code
int main()
{
    int a = 10, b = 7;
 
    cout << countBits(a, b);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
class GFG
{
 
// Function to return the count of bits
// to be flipped to convert a to b
static int countBits(int a, int b)
{
 
    // To store the required count
    int count = 0;
 
    // Loop until both of them become zero
    while (a > 0 || b > 0)
    {
 
        // Store the last bits in a
        // as well as b
        int last_bit_a = a & 1;
        int last_bit_b = b & 1;
 
        // If the current bit is not same
        // in both the integers
        if (last_bit_a != last_bit_b)
            count++;
 
        // Right shift both the integers by 1
        a = a >> 1;
        b = b >> 1;
    }
 
    // Return the count
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int a = 10, b = 7;
 
    System.out.println(countBits(a, b));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the approach
 
# Function to return the count of bits
# to be flipped to convert a to b
def countBits(a, b):
 
    # To store the required count
    count = 0
 
    # Loop until both of them become zero
    while (a or b):
 
        # Store the last bits in a
        # as well as b
        last_bit_a = a & 1
        last_bit_b = b & 1
 
        # If the current bit is not same
        # in both the integers
        if (last_bit_a != last_bit_b):
            count += 1
 
        # Right shift both the integers by 1
        a = a >> 1
        b = b >> 1
 
    # Return the count
    return count
 
# Driver code
a = 10
b = 7
 
print(countBits(a, b))
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
// Function to return the count of bits
// to be flipped to convert a to b
static int countBits(int a, int b)
{
 
    // To store the required count
    int count = 0;
 
    // Loop until both of them become zero
    while (a > 0 || b > 0)
    {
 
        // Store the last bits in a
        // as well as b
        int last_bit_a = a & 1;
        int last_bit_b = b & 1;
 
        // If the current bit is not same
        // in both the integers
        if (last_bit_a != last_bit_b)
            count++;
 
        // Right shift both the integers by 1
        a = a >> 1;
        b = b >> 1;
    }
 
    // Return the count
    return count;
}
 
// Driver code
public static void Main(String[] args)
{
    int a = 10, b = 7;
 
    Console.WriteLine(countBits(a, b));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
3