Add HammingDistance algorithm (#5706)

This commit is contained in:
Hardik Pawar
2024-10-12 12:01:04 +05:30
committed by GitHub
parent c0ffbb0e45
commit bf0377f44b
3 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package com.thealgorithms.bitmanipulation;
/**
* The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
* Given two integers x and y, calculate the Hamming distance.
* Example:
* Input: x = 1, y = 4
* Output: 2
* Explanation: 1 (0001) and 4 (0100) have 2 differing bits.
*
* @author Hardvan
*/
public final class HammingDistance {
private HammingDistance() {
}
/**
* Calculates the Hamming distance between two integers.
* The Hamming distance is the number of differing bits between the two integers.
*
* @param x The first integer.
* @param y The second integer.
* @return The Hamming distance (number of differing bits).
*/
public static int hammingDistance(int x, int y) {
int xor = x ^ y;
return Integer.bitCount(xor);
}
}