# Count number of bits to be flipped to convert A to B

Given two numbers ‘a’ and b’. Write a program to count number of bits needed to be flipped to convert ‘a’ to ‘b’.

**Example : **

Input : a = 10, b = 20 Output : 4 Binary representation of a is 00001010 Binary representation of b is 00010100 We need to flip highlighted four bits in a to make it b. Input : a = 7, b = 10 Output : 3 Binary representation of a is 00000111Binary representation of b is 00001010 We need to flip highlighted three bits in a to make it b.

1. Calculate XOR of A and B. a_xor_b = A ^ B 2. Count the set bits in the above calculated XOR result. countSetBits(a_xor_b)

XOR of two number will have set bits only at those places where A differs from B.

## C++

`// Count number of bits to be flipped ` `// to convert A into B ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function that count set bits ` `int` `countSetBits(` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `count++; ` ` ` `n &= (n-1); ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Function that return count of ` `// flipped number ` `int` `FlippedCount(` `int` `a, ` `int` `b) ` `{ ` ` ` `// Return count of set bits in ` ` ` `// a XOR b ` ` ` `return` `countSetBits(a^b); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 10; ` ` ` `int` `b = 20; ` ` ` `cout << FlippedCount(a, b)<<endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Count number of bits to be flipped ` `// to convert A into B ` `import` `java.util.*; ` ` ` `class` `Count { ` ` ` ` ` `// Function that count set bits ` ` ` `public` `static` `int` `countSetBits(` `int` `n) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `while` `(n != ` `0` `) { ` ` ` `count++; ` ` ` `n &=(n-` `1` `); ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Function that return count of ` ` ` `// flipped number ` ` ` `public` `static` `int` `FlippedCount(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `// Return count of set bits in ` ` ` `// a XOR b ` ` ` `return` `countSetBits(a ^ b); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `a = ` `10` `; ` ` ` `int` `b = ` `20` `; ` ` ` `System.out.print(FlippedCount(a, b)); ` ` ` `} ` `} ` ` ` `// This code is contributed by rishabh_jain ` |

*chevron_right*

*filter_none*

## Python3

`# Count number of bits to be flipped ` `# to convert A into B ` ` ` `# Function that count set bits ` `def` `countSetBits( n ): ` ` ` `count ` `=` `0` ` ` `while` `n: ` ` ` `count ` `+` `=` `1` ` ` `n &` `=` `(n` `-` `1` `) ` ` ` `return` `count ` ` ` `# Function that return count of ` `# flipped number ` `def` `FlippedCount(a , b): ` ` ` ` ` `# Return count of set bits in ` ` ` `# a XOR b ` ` ` `return` `countSetBits(a^b) ` ` ` `# Driver code ` `a ` `=` `10` `b ` `=` `20` `print` `(FlippedCount(a, b)) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// Count number of bits to be ` `// flipped to convert A into B ` `using` `System; ` ` ` `class` `Count { ` ` ` ` ` `// Function that count set bits ` ` ` `public` `static` `int` `countSetBits(` `int` `n) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(n != 0) { ` ` ` `count++; ` ` ` `n &= (n-1); ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Function that return ` ` ` `// count of flipped number ` ` ` `public` `static` `int` `FlippedCount(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `// Return count of set ` ` ` `// bits in a XOR b ` ` ` `return` `countSetBits(a ^ b); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `a = 10; ` ` ` `int` `b = 20; ` ` ` `Console.WriteLine(FlippedCount(a, b)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Count number of bits to be ` `// flipped to convert A into B ` ` ` `// Function that count set bits ` `function` `countSetBits(` `$n` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `while` `(` `$n` `) ` ` ` `{ ` ` ` `$count` `+= 1; ` ` ` `$n` `&= (n-1); ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` `// Function that return ` `// count of flipped number ` `function` `FlippedCount(` `$a` `, ` `$b` `) ` `{ ` ` ` `// Return count of set ` ` ` `// bits in a XOR b ` ` ` `return` `countSetBits(` `$a` `^ ` `$b` `); ` `} ` ` ` `// Driver code ` `$a` `= 10; ` `$b` `= 20; ` `echo` `FlippedCount(` `$a` `, ` `$b` `); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

4

Thanks to **Sahil Rajput** for providing above implementation.

To get the set bit count please see this post: Count set bits in an integer

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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count number of bits to be flipped to convert A to B | Set-2
- 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
- Check if bits of a number has count of consecutive set bits in increasing order
- Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart
- Count Set-bits of number using Recursion
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count unset bits of a number
- Count total bits in a number
- Count number of set bits in a range using bitset
- Count of pairs in an Array with same number of set bits
- Program to count number of set bits in an (big) array
- Count number of bits changed after adding 1 to given N
- Count pairs of elements such that number of set bits in their AND is B[i]
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Maximum number of set bits count in a K-size substring of a Binary String
- Maximum number of 0s that can be flipped such that Array has no adjacent 1s
- Find zeroes to be flipped so that number of consecutive 1's is maximized
- Toggle bits of a number except first and last bits
- Count set bits in a range