Cuente el número de bits que se invertirán para convertir A en B | Conjunto-2

Dados dos enteros A y B , la tarea es contar el número de bits necesarios para convertir A en B .
Ejemplos: 
 

Entrada: A = 10, B = 7 
Salida:
binario(10) = 1010 
binario(7) = 0111 
10 1 0  
01 1 1 
3 bits deben invertirse.
Entrada: A = 8, B = 7 
Salida:
 

Enfoque: Ya se ha discutido aquí un enfoque para resolver este problema . Aquí, el recuento de bits que deben invertirse se puede encontrar haciendo coincidir todos los bits en ambos enteros uno por uno. Si el bit bajo consideración difiere, entonces incremente el conteo.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
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

<script>
 
// Javascript implementation of the approach
 
// Function to return the count of bits
// to be flipped to convert a to b
function countBits(a, b)
{
 
    // To store the required count
    var count = 0;
 
    // Loop until both of them become zero
    while (a || b) {
 
        // Store the last bits in a
        // as well as b
        var last_bit_a = a & 1;
        var 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
    var a = 10, b = 7;
    document.write(countBits(a, b));
 
</script>
Producción: 

3

 

Complejidad del tiempo: O(min(log a, log b))

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por vishal_kr_chopra y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *