def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
return bin(x^y).count('1')
Python 1 line 49ms

My code takes 49ms as well. Maybe it is how python implements count().
class Solution(object): def hammingDistance(self, x, y): """ :type x: int :type y: int :rtype: int """ target = x ^ y return Solution.count_ones(target) @staticmethod def round(num, mask, c): return (num & mask) + (num >> (1 << c) & mask) @staticmethod def count_ones(num): num = Solution.round(num, 0x55555555, 0) num = Solution.round(num, 0x33333333, 1) num = Solution.round(num, 0x0f0f0f0f, 2) num = Solution.round(num, 0x00ff00ff, 3) num = Solution.round(num, 0x0000ffff, 4) return num

@ian34 i am really new to python. What is '^' in x ^ y. Are we subtracting both to find number of ones ?


@bhavya12 it is XOR operator, 0101 ^ 0001 = 0100 , in every bite same value get '1', different value get '0'



@bhavya12 It's an XOR. Imagine that it lines up x and y's bits and then keeps the ones that don't match up.

@NoAnyLove said in Python 1 line 49ms:
=
Hi, friend,
Could you tell me how the '>>= ' operator works?
I cannot find information about it online.Thank you
