- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathHammingDistance.java
29 lines (27 loc) · 871 Bytes
/
HammingDistance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
packagecom.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
*/
publicfinalclassHammingDistance {
privateHammingDistance() {
}
/**
* 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).
*/
publicstaticinthammingDistance(intx, inty) {
intxor = x ^ y;
returnInteger.bitCount(xor);
}
}