Given two integers A and B, the task is to count the number of bits needed to be flipped to convert A to B.
Examples:
Input: A = 10, B = 7
Output: 3
binary(10) = 1010
binary(7) = 0111
1010
0111
3 bits need to be flipped.Input: A = 8, B = 7
Output: 4
Approach: An approach to solve this problem has already been discussed here. Here, the count of bits that need to be flipped can be found by matching all the bits in both the integers one by one. If the bit under consideration differs then increment the count.
Below is the implementation of the above approach:
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 = 10b = 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 |
3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count number of bits to be flipped to convert A to B
- Count of total bits toggled/flipped in binary representation of 0 to N
- Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z
- Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart
- Check if the number is valid when flipped upside down
- Maximum number of 0s that can be flipped such that Array has no adjacent 1s
- Check if bits of a number has count of consecutive set bits in increasing order
- Toggle bits of a number except first and last bits
- Print numbers having first and last bits as the only set bits
- Check if all bits can be made same by flipping two consecutive bits
- Program to count number of set bits in an (big) array
- Count total bits in a number
- Count unset bits of a number
- Count number of bits changed after adding 1 to given N
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count number of set bits in a range using bitset
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count Set-bits of number using Recursion
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Count of pairs in an Array with same number of set bits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

