Problem statement

https://binarysearch.com/problems/Hamming-Distance/

Solution

Equal to Leetcode 0461. Hamming Distance.

Complexity

It is O(log n) for time and O(1) for space.

Code

class Solution:
    def solve(self, x, y):
        ans, t = 0, x^y 
        while t:
            t, ans = t & (t-1), ans + 1
        return ans