# Hamming distance between two Integers

Given two integers, the task is to find the hamming distance between two integers. Hamming Distance between two integers is the number of bits which are different at same position in both numbers.

**Examples:**

Input:n1 = 9, n2 = 14Output:3 9 = 1001, 14 = 1110 No. of Different bits = 3Input:n1 = 4, n2 = 8Output:2

**Approach:**

- Calculate the XOR of two numbers.
- Count the number of set bits.

Below is the implementation of above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate hamming distance ` `int` `hammingDistance(` `int` `n1, ` `int` `n2) ` `{ ` ` ` `int` `x = n1 ^ n2; ` ` ` `int` `setBits = 0; ` ` ` ` ` `while` `(x > 0) { ` ` ` `setBits += x & 1; ` ` ` `x >>= 1; ` ` ` `} ` ` ` ` ` `return` `setBits; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n1 = 9, n2 = 14; ` ` ` `cout << hammingDistance(9, 14) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `class` `GFG ` `{ ` ` ` `// Function to calculate hamming distance ` `static` `int` `hammingDistance(` `int` `n1, ` `int` `n2) ` `{ ` ` ` `int` `x = n1 ^ n2; ` ` ` `int` `setBits = ` `0` `; ` ` ` ` ` `while` `(x > ` `0` `) ` ` ` `{ ` ` ` `setBits += x & ` `1` `; ` ` ` `x >>= ` `1` `; ` ` ` `} ` ` ` ` ` `return` `setBits; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n1 = ` `9` `, n2 = ` `14` `; ` ` ` `System.out.println(hammingDistance(n1, n2)); ` `} ` `} ` ` ` `// This code is contributed by Bilal ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of above approach ` ` ` `# Function to calculate hamming distance ` `def` `hammingDistance(n1, n2) : ` ` ` ` ` `x ` `=` `n1 ^ n2 ` ` ` `setBits ` `=` `0` ` ` ` ` `while` `(x > ` `0` `) : ` ` ` `setBits ` `+` `=` `x & ` `1` ` ` `x >>` `=` `1` ` ` ` ` `return` `setBits ` ` ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `n1 ` `=` `9` ` ` `n2 ` `=` `14` ` ` `print` `(hammingDistance(` `9` `, ` `14` `)) ` ` ` `# this code is contributed by Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `class` `GFG ` `{ ` ` ` `// Function to calculate ` `// hamming distance ` `static` `int` `hammingDistance(` `int` `n1, ` `int` `n2) ` `{ ` ` ` `int` `x = n1 ^ n2; ` ` ` `int` `setBits = 0; ` ` ` ` ` `while` `(x > 0) ` ` ` `{ ` ` ` `setBits += x & 1; ` ` ` `x >>= 1; ` ` ` `} ` ` ` ` ` `return` `setBits; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `n1 = 9, n2 = 14; ` ` ` `System.Console.WriteLine(hammingDistance(n1, n2)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?PHP ` `// PHP implementation of above approach ` ` ` `// Function to calculate hamming distance ` `function` `hammingDistance(` `$n1` `, ` `$n2` `) ` `{ ` ` ` `$x` `= ` `$n1` `^ ` `$n2` `; ` ` ` `$setBits` `= 0; ` ` ` ` ` `while` `(` `$x` `> 0) ` ` ` `{ ` ` ` `$setBits` `+= ` `$x` `& 1; ` ` ` `$x` `>>= 1; ` ` ` `} ` ` ` ` ` `return` `$setBits` `; ` `} ` ` ` `// Driver code ` `$n1` `= 9; ` `$n2` `= 14; ` `echo` `(hammingDistance(9, 14)); ` ` ` `// This code is contributed by Smitha ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

**Note:** No. of set bits can be count using __builtin_popcount() function.

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:

- Sum of Hamming difference of consecutive numbers from 0 to N | Set 2
- Maximum distance between two 1's in Binary representation of N
- Maximize distance between any two consecutive 1's after flipping M 0's
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Distance between two nodes of binary tree with node values from 1 to N
- Check if the XOR of an array of integers is Even or Odd
- How to sum two integers without using arithmetic operators in C/C++?
- Sum of consecutive bit differences of first N non-negative integers
- Compare two integers without using any Comparison operator
- Bitwise recursive addition of two integers
- Detect if two integers have opposite signs
- Smallest of three integers without comparison operators
- Divide two integers without using multiplication, division and mod operator
- Check if given four integers (or sides) make rectangle
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Count of integers up to N which represent a Binary number
- Compute the minimum or maximum of two integers without branching
- Find maximum XOR of given integer in a stream of integers
- Count integers in an Array which are multiples their bits counts
- Queries to find the count of integers in a range that contain the given pattern

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.