Count number of bits to be flipped to convert a to b

 

Problem Statement:- Program to count the number of bits needed to be flipped to convert a to b.

Sample Input/Output:-


Sample Input First:

7

11


Sample Output First: 

2

/*7 Binary=>0111

 11 Binary=>1011

*/

Sample Input Second: 

21

8

Sample Output Second: 

4

/*21 Binary=>010101

  37 Binary=>001000

*/


Data requirement:-


  Input Data:- a, b


  Output Data:-Count_flipped_bit


  Additional Data:- num


Program in C

Here is the source code of the C Program to count the number of bits needed to be flipped to convert a to b.


Code:

#include <stdio.h>
int main()
{
    int ab;
    /* Get the value of a and b*/
    printf("Enter the value of A and B:");
    scanf("%d %d", &a, &b);
    /* Calculating xor of a and b */
    int num = a ^ b;
    int Count_flipped_bit = 0;
    /*Counting Number of set bit present*/
    while (num != 0)
    {
        num = num & (num - 1);
        Count_flipped_bit++;
    }
    printf("Number of bits needed to be flipped to convert a to b is: %d"Count_flipped_bit);
    return 0;
}


Input/Output:

Enter the value of A and B:7

11

Number of bits needed to be flipped to convert a to b is: 2


Program in C++

Here is the source code of the C++ Program to count the number of bits needed to be flipped to convert a to b.


Code:

#include <iostream>
using namespace std;
int main()
{
    int ab;
    /* Get the value of a and b*/
    cout<<"Enter the value of A and B:";
    cin>>a>>b;
    /* Calculating xor of a and b */
    int num = a ^ b;
    int Count_flipped_bit = 0;
    /*Counting Number of set bit present*/
    while (num != 0)
    {
        num = num & (num - 1);
        Count_flipped_bit++;
    }
    cout<<"Number of bits needed to be flipped to convert a to b is: "<<Count_flipped_bit;
    return 0;
}


Input/Output:

Enter the value of A and B:21

8

Number of bits needed to be flipped to convert a to b is: 4


Program in Java

Here is the source code of the Java Program to count the number of bits needed to be flipped to convert a to b.


Code:

import java.util.Scanner;

public class CountFlippedBits {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        /* Get the value of a and b */
        System.out.print("Enter the value of A and B:");
        int a = sc.nextInt();
        int b = sc.nextInt();
        /* Calculating xor of a and b */
        int num = a ^ b;
        int Count_flipped_bit = 0;
        /* Counting Number of set bit present */
        while (num != 0) {
            num = num & (num - 1);
            Count_flipped_bit++;
        }
        System.out.print("Number of bits needed to be flipped to convert a to b is: " + Count_flipped_bit);
        sc.close();
    }
}


Input/Output:

Enter the value of A and B:77

177

Number of bits needed to be flipped to convert a to b is: 6


Program in Python

Here is the source code of the Python Program to count the number of bits needed to be flipped to convert a to b.


Code:

# Get the value of a and b
ab = map(intinput("Enter the value of A and B:").split())
# a,b=int(input("Enter the value of A and B:"))
# Calculating xor of a and b
num = a ^ b
Count_flipped_bit = 0
# Counting Number of set bit present
while (num != 0):
    num = num & (num - 1)
    Count_flipped_bit += 1
print("Number of bits needed to be flipped to convert a to b is:"Count_flipped_bit)



Input/Output:

Enter the value of A and B:999 333

Number of bits needed to be flipped to convert a to b is: 5

Most Recommend Questions:-






More Questions:-


Post a Comment

0 Comments