Hamming Distance LeetCode solution in Python
1class Solution:2def hammingDistance(self, x: int, y: int) -> int:3# First, using XOR Bitwise Operator, we take all distinct set bits.4z = x ^ y5# We inicialize our answer with zero.6ans = 07# Iterate while our z is not zero.8while z:9# Every iteration we add one to our answer.10ans += 111# Using the expression z & (z - 1), we erase the lowest set bit in z.12z &= z - 113return ans