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.

Note:
0 ≤ x, y < 231.


Example:

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.

Difficulty: introductory

Starter code:

class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        

Solutions:
class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         x = x ^ y
         y = 0
         while (x):
             y += 1
             x &= x-1
         return y


class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         if x == y:
             return 0
         bin_x, bin_y = bin(x)[2:], bin(y)[2:]
         if x < y:
             bin_x = bin_x.zfill(len(bin_y))
         else:
             bin_y = bin_y.zfill(len(bin_x))
         return sum([abs(int(bin_x[i]) - int(bin_y[i])) for i in range(len(bin_x))])

class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         count = 0
         for i in range(32):
             count += (x & 1) ^ (y & 1)
             x >>= 1
             y >>= 1
         return count

class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         d = bin(x^y)
         return d.count('1')


class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         x_b = '{:32b}'.format(x).replace(' ', '0')
         y_b = '{:32b}'.format(y).replace(' ', '0')
         
         count = 0
         for i in range(len(x_b)):
             if x_b[i] != y_b[i]:
                 count += 1
         return count


class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         # 035b is the code for formatting the int as a binary of 35 characters with 0 as left hand padding
         x_bin = format(x, '#035b')
         y_bin = format(y, '#035b')
         
         print("x_bin: ", x_bin)
         print("y_bin: ", y_bin)
         
         hammingDistance = 0
         
         for idx, val in enumerate (x_bin):
             print("val: ", val)
             print("y_bin[idx]: ", y_bin[idx])
             if val != y_bin[idx]:
                 hammingDistance += 1
                 
         return hammingDistance

class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         xor = x ^ y
         count = 0
         for _ in range(32):
             count += xor & 1
             xor = xor >> 1
         return count

class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         
         count = 0
         while x > 0 or y > 0:
             if (x & 1) != (y & 1):
                 count += 1
             
             if x > 0:
                 x = x >> 1
             
             if y > 0:
                 y = y >> 1
         
         return count

class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         x_bits = [int(i) for i in bin(x)[2:]]
         y_bits = [int(j) for j in bin(y)[2:]]
         digit_diff = abs(len(x_bits)-len(y_bits))
 
         if len(y_bits) > len(x_bits):
             x_bits = [0] * digit_diff + x_bits
         elif len(x_bits) > len(y_bits):
             y_bits = [0] * digit_diff + y_bits
 
         hamming_dist = 0
         for i in range(0, len(x_bits)):
             if x_bits[i] != y_bits[i]:
                 hamming_dist += 1
 
         return hamming_dist


class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         xor_result = x^y
         count = 0
         while xor_result > 0:
             count += xor_result & 1
             xor_result = xor_result >> 1
         
         return count
             
         
         


class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         
         hamming_distance=0
         
         while(x != 0 or y != 0):
             b1 = x & 1
             b2 = y & 1
             if(b1 != b2):
                 hamming_distance = hamming_distance+1
             x = x >> 1
             y = y >> 1
         
         return hamming_distance

class Solution:
     def hammingDistance(self, x, y):
         """
         :type x: int
         :type y: int
         :rtype: int
         """
         return bin(x^y).count('1')