Hamming Distance

🏠 ⬅️ ➡️

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, return the Hamming distance between them.

Example 1:

Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different.

Example 2:

Input: x = 3, y = 1 Output: 1

Constraints:

  • 0 <= x, y <= 231 - 1

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program hammingDistance
    implicit none

    integer :: x, y, hammingDistance

    ! Example 1
    x = 1
    y = 4
    print '(A, I0)', 'Hamming distance between ', x, 'and ', y, 'is ', hammingDistance(x, y)

    ! Example 2
    x = 3
    y = 1
    print '(A, I0)', 'Hamming distance between ', x, 'and ', y, 'is ', hammingDistance(x, y)

contains

    function hammingDistance(x, y) result(hammingDist)
        implicit none
        integer, intent(in) :: x, y
        integer :: hammingDist

        hammingDist = count(bit_xor(x, y) /= 0)
    end function hammingDistance

end program hammingDistance
Compiled
Executed
Correct
!-------------------------------------------------------------------------------
! Copyright (c) 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029,
! 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041,
! 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050, 2051, 2052, 2053,
! 2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065,
! 2066, 2067, 2068, 2069, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077,
! 2078, 2079, 2080, 2081, 2082, 2083, 2084, 2085, 2086, 2087, 2088, 2089,
! 2090, 2091, 2092, 2093, 2094, 2095, 2096, 2097, 2098, 2099, 2100, 2101,
! 2102, 2103, 2104, 2105, 2106, 2107, 2108, 2109, 2110, 2111, 2112, 2113,
! 2114, 2115, 2116, 2117, 2118, 2119, 2120, 2121, 2122, 2123, 2124, 2125,
! 2126, 2127, 2128, 2129, 2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137,
! 2138, 2139, 2140, 2141, 2142, 2143, 2144, 2145, 2146, 2147, 2148, 2149,
! 2150, 2151, 2152, 2153, 2154, 2155, 2156, 2157, 2158, 2159, 2160, 2161,
! 2162, 2163, 2164, 2165, 2166, 2167, 2168, 2169, 2170, 2171, 2172, 2173,
! 2174, 2175, 2176, 2177, 2178, 2179, 2180, 2181, 2182, 2183
🌐 Data from online sources
def hammingDistance(x: int, y: int) -> int:
    xorResult = x ^ y
    dist = 0
    while xorResult:
        dist += xorResult & 1
        xorResult >>= 1
    return dist

We can start by taking the XOR of x and y. A XOR B value will have a bit set only if both A and B bits are different at that position. We then iterate through the bits of the XOR result, counting the number of set bits, which should give us the Hamming distance required.

  1. Calculate xorResult by XORing x and y.
  2. Initialize dist to 0.
  3. While xorResult is not 0, do the following: a. Add the least significant bit of xorResult to dist. b. Right shift xorResult by 1.
  4. Return dist.

This algorithm performs bitwise operations and loops through the bits of xorResult, which has a maximum of 31 bits due to the constraint 0 <= x, y <= 2^31 - 1. Thus, the time complexity is O(31) = O(1).

🌐 Data from online sources
int hammingDistance(int x, int y) {
    int dist = 0, xorResult = x ^ y;
    while (xorResult) {
        dist += xorResult & 1;
        xorResult >>= 1;
    }
    return dist;
}

We can start by taking the XOR of x and y. A XOR B value will have a bit set only if both A and B bits are different at that position. We then iterate through the bits of the XOR result, counting the number of set bits, which should give us the Hamming distance required.

  1. Calculate xorResult by XORing x and y.
  2. Initialize dist to 0.
  3. While xorResult is not 0, do the following: a. Add the least significant bit of xorResult to dist. b. Right shift xorResult by 1.
  4. Return dist.

This algorithm performs bitwise operations and loops through the bits of xorResult, which has a maximum of 31 bits due to the constraint 0 <= x, y <= 2^31 - 1. Thus, the time complexity is O(31) = O(1).