Hamming distance between two Integers - GeeksforGeeks

16 Mar, 2022
Given two integers, the task is to find the hamming distance between two integers. Hamming Distance between two integers is the number of bits that are different at the same position in both numbers. Examples:  
Input: n1 = 9, n2 = 14
Output: 3
9 = 1001, 14 = 1110
No. of Different bits = 3

Input: n1 = 4, n2 = 8
Output: 2
 
Approach:  
Calculate the XOR of two numbers.Count the number of set bits.
Calculate the XOR of two numbers.
Count the number of set bits.
Below is the implementation of above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to calculate hamming distanceint hammingDistance(int n1, int n2){    int x = n1 ^ n2;    int setBits = 0;     while (x > 0) {        setBits += x & 1;        x >>= 1;    }     return setBits;} // Driver codeint main(){    int n1 = 9, n2 = 14;    cout << hammingDistance(9, 14) << endl;     return 0;}
// Java implementation of above approachclass GFG{ // Function to calculate hamming distancestatic int hammingDistance(int n1, int n2){    int x = n1 ^ n2;    int setBits = 0;     while (x > 0)    {        setBits += x & 1;        x >>= 1;    }     return setBits;} // Driver codepublic static void main(String[] args){    int n1 = 9, n2 = 14;    System.out.println(hammingDistance(n1, n2));}} // This code is contributed by Bilal
# Python3 implementation of above approach # Function to calculate hamming distancedef 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
// C# implementation of above approachclass GFG{ // Function to calculate// hamming distancestatic int hammingDistance(int n1, int n2){    int x = n1 ^ n2;    int setBits = 0;     while (x > 0)    {        setBits += x & 1;        x >>= 1;    }     return setBits;} // Driver codestatic void Main(){    int n1 = 9, n2 = 14;    System.Console.WriteLine(hammingDistance(n1, n2));}} // This code is contributed by mits
<?PHP// PHP implementation of above approach // Function to calculate hamming distancefunction 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?>
<script> // Javascript implementation of above approach // Function to calculate hamming distancefunction hammingDistance(n1, n2){    let x = n1 ^ n2;    let setBits = 0;     while (x > 0) {        setBits += x & 1;        x >>= 1;    }     return setBits;} // Driver code    let n1 = 9, n2 = 14;    document.write(hammingDistance(9, 14)); </script>
3
Note: No. of set bits can be count using __builtin_popcount() function.
Approach 2:
1. Calculate the maximum of both the numbers.
2.Check the set bits for both at each position.
C++
Python3
Javascript
#include <bits/stdc++.h>using namespace std;int hammingDistance(int x, int y){    int ans = 0;    int m = max(x, y);    while (m) {        int c1 = x & 1;        int c2 = y & 1;        if (c1 != c2)            ans += 1;        m = m >> 1;        x = x >> 1;        y = y >> 1;    }    return ans;}int main(){    int n1 = 4, n2 = 8;    int hdist = hammingDistance(n1, n2);    cout << hdist << endl;    return 0;}
# Python3 code for the same approachdef hammingDistance(x, y):    ans = 0   m = max(x, y)   while (m):      c1 = x & 1      c2 = y & 1      if (c1 != c2):         ans += 1      m = m >> 1      x = x >> 1      y = y >> 1   return ans # driver programn1 = 4n2 = 8hdist = hammingDistance(n1, n2)print(hdist) # This code is contributed by shinjanpatra
<script> // JavaScript code for the same approach function hammingDistance(x,y){    let ans = 0;    let m = Math.max(x, y);    while (m) {        let c1 = x & 1;        let c2 = y & 1;        if (c1 != c2)            ans += 1;        m = m >> 1;        x = x >> 1;        y = y >> 1;    }    return ans;} // driver programlet n1 = 4,n2 = 8;let hdist = hammingDistance(n1, n2);document.write(hdist,"</br>"); // This code is contributed by shinjanpatra </script>
2
 
bilal-hungund
Mithun Kumar
Smitha Dinesh Semwal
subham348
adityamutharia
shinjanpatra
Bitwise-XOR
setBitCount
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Set, Clear and Toggle a given bit of a number in C
Program to find parity
Hamming code Implementation in C/C++
Check whether K-th bit is set or not
Write an Efficient Method to Check if a Number is Multiple of 3
Implementation of Bit Stuffing and Bit Destuffing
Builtin functions of GCC compiler
Find XOR of all elements in an Array
Swap bits in a given number
Count total bits in a number