# Number of mismatching bits in the binary representation of two integers

Given two integers(less than 2^31) A and B. The task is to find the number of bits that are different in their binary representation.

**Examples:**

Input :A = 12, B = 15Output :Number of different bits : 2Explanation:The binary representation of 12 is 1100 and 15 is 1111. So, the number of different bits are 2.Input :A = 3, B = 16Output :Number of different bits : 3

**Approach:**

- Run a loop from ‘0’ to ’31’ and right shift the bits of A and B by ‘i’ places, then check whether the bit at the ‘0th’ position is different.
- If the bit is different then increase the count.
- As the numbers are less than 2^31, we only have to run the loop ’32’ times i.e. from ‘0’ to ’31’.
- We can get the 1st bit if we bitwise AND the number by 1.
- At the end of the loop display the count.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// compute number of different bits ` `void` `solve(` `int` `A, ` `int` `B) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// since, the numbers are less than 2^31 ` ` ` `// run the loop from '0' to '31' only ` ` ` `for` `(` `int` `i = 0; i < 32; i++) { ` ` ` ` ` `// right shift both the numbers by 'i' and ` ` ` `// check if the bit at the 0th position is different ` ` ` `if` `(((A >> i) & 1) != ((B >> i) & 1)) { ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `cout << ` `"Number of different bits : "` `<< count << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `A = 12, B = 15; ` ` ` ` ` `// find number of different bits ` ` ` `solve(A, B); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// compute number of different bits ` `static` `void` `solve(` `int` `A, ` `int` `B) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// since, the numbers are less than 2^31 ` ` ` `// run the loop from '0' to '31' only ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `32` `; i++) { ` ` ` ` ` `// right shift both the numbers by 'i' and ` ` ` `// check if the bit at the 0th position is different ` ` ` `if` `(((A >> i) & ` `1` `) != ((B >> i) & ` `1` `)) { ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `System.out.println(` `"Number of different bits : "` `+ count); ` `} ` ` ` `// Driver code ` ` ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `A = ` `12` `, B = ` `15` `; ` ` ` ` ` `// find number of different bits ` ` ` `solve(A, B); ` ` ` ` ` `} ` `} ` `// this code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# compute number of different bits ` `def` `solve( A, B): ` ` ` ` ` `count ` `=` `0` ` ` ` ` `# since, the numbers are less than 2^31 ` ` ` `# run the loop from '0' to '31' only ` ` ` `for` `i ` `in` `range` `(` `0` `,` `32` `): ` ` ` ` ` `# right shift both the numbers by 'i' and ` ` ` `# check if the bit at the 0th position is different ` ` ` `if` `((( A >> i) & ` `1` `) !` `=` `(( B >> i) & ` `1` `)): ` ` ` `count` `=` `count` `+` `1` ` ` ` ` ` ` ` ` `print` `(` `"Number of different bits :"` `,count) ` ` ` ` ` `# Driver code ` `A ` `=` `12` `B ` `=` `15` ` ` `# find number of different bits ` `solve( A, B) ` ` ` ` ` `# This code is contributed by ihritik ` ` ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` ` ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// compute number of different bits ` ` ` `static` `void` `solve(` `int` `A, ` `int` `B) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// since, the numbers are less than 2^31 ` ` ` `// run the loop from '0' to '31' only ` ` ` `for` `(` `int` `i = 0; i < 32; i++) { ` ` ` ` ` `// right shift both the numbers by 'i' and ` ` ` `// check if the bit at the 0th position is different ` ` ` `if` `(((A >> i) & 1) != ((B >> i) & 1)) { ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `Console.WriteLine(` `"Number of different bits : "` `+ count); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `A = 12, B = 15; ` ` ` ` ` `// find number of different bits ` ` ` `solve(A, B); ` ` ` ` ` `} ` ` ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// compute number of different bits ` `function` `solve(` `$A` `, ` `$B` `) ` `{ ` ` ` `$count` `= 0; ` ` ` ` ` `// since, the numbers are less than 2^31 ` ` ` `// run the loop from '0' to '31' only ` ` ` `for` `(` `$i` `= 0; ` `$i` `< 32; ` `$i` `++) { ` ` ` ` ` `// right shift both the numbers by 'i' and ` ` ` `// check if the bit at the 0th position is different ` ` ` `if` `(((` `$A` `>> ` `$i` `) & 1) != ((` `$B` `>> ` `$i` `) & 1)) { ` ` ` `$count` `++; ` ` ` `} ` ` ` `} ` ` ` ` ` `echo` `"Number of different bits : $count"` `; ` `} ` ` ` `// Driver code ` `$A` `= 12; ` `$B` `= 15; ` ` ` `// find number of different bits ` `solve(` `$A` `, ` `$B` `); ` ` ` `// This code is contributed by ihritik ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Number of different bits : 2

## Recommended Posts:

- Prime Number of Set Bits in Binary Representation | Set 1
- Binary representation of a given number
- Count number of trailing zeros in Binary representation of a number using Bitset
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Binary representation of previous number
- Next greater number than N with exactly one bit different in binary representation of N
- Largest number with binary representation is m 1's and m-1 0's
- Number of integers with odd number of set bits
- Number of leading zeros in binary representation of a given number
- Find the n-th number whose binary representation is a palindrome
- Check if binary representation of a number is palindrome
- Occurrences of a pattern in binary representation of a number
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Find consecutive 1s of length >= n in binary representation of a number
- Check if binary representation of a given number and its complement are anagram

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.