Problem Statement:- Program to count the number of bits needed to be flipped to convert a to b.
Sample Input/Output:-
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 a, b; /* 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
#include <stdio.h>int main(){ int a, b; /* 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;}
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 a, b; /* 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;}
#include <iostream>using namespace std;int main(){ int a, b; /* 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
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(); }}
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 ba, b = map(int, input("Enter the value of A and B:").split())# a,b=int(input("Enter the value of A and B:"))# Calculating xor of a and bnum = a ^ bCount_flipped_bit = 0# Counting Number of set bit presentwhile (num != 0): num = num & (num - 1) Count_flipped_bit += 1print("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
# Get the value of a and ba, b = map(int, input("Enter the value of A and B:").split())# a,b=int(input("Enter the value of A and B:"))# Calculating xor of a and bnum = a ^ bCount_flipped_bit = 0# Counting Number of set bit presentwhile (num != 0): num = num & (num - 1) Count_flipped_bit += 1print("Number of bits needed to be flipped to convert a to b is:", Count_flipped_bit)
0 Comments
Please do not Enter any spam link in the comment box