# 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:711 Sample Output First: 2/*7 Binary=>0111 11 Binary=>1011*/ Sample Input Second: 218 Sample Output Second: 4/*21 Binary=>010101  37 Binary=>001000*/

#### Data requirement:-

Input Data:- a, b

Output Data:-Count_flipped_bit

Program in C

Code:

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:

### ``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:77177Number of bits needed to be flipped to convert a to b is: 6

Program in Python

Code: