<jupyter_start><jupyter_text>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:```javascript0 ≤ x, y < 231.```Example:```javascriptInput: x = 1, y = 4Output: 2```Explanation:```javascript1   (0 0 0 1)4   (0 1 0 0)       ↑   ↑```The above arrows point to positions where the corresponding bits are different.<jupyter_code># Approach #1 : We could broadcast them into binary bits & count number of different bits, like so -
import numpy as np
class Solution(object):
    def hammingDistance(self, x, y):
        r = (1 << np.arange(8))[:,None]
        return np.count_nonzero( (x & r) != (y & r) )
# Approach #2 : Using bitwise-xor operation, we can find out the number of different binary bits between a and b -
import numpy as np
class Solution(object):
    def hammingDistance(self, x, y):
        r = (1 << np.arange(8))[:,None]
        '''make a column array
    array([[  1],
       [  2],
       [  4],
       [  8],
       [ 16],
       [ 32],
       [ 64],
       [128]])
       First, xor function for x,y,
       Then and with r, find the numbers constitutes the remainer. 2,4,8, ...
       Finally count non-Zeros.
        '''
        return np.count_nonzero((np.bitwise_xor(x,y) & r) != 0)
if __name__ == '__main__':
    print(Solution().hammingDistance(2,4))<jupyter_output>2
{'fraction_non_alphanumeric': 0.1285310734463277, 'fraction_numerical': 0.03036723163841808, 'mean_word_length': 2.925207756232687, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '623074', 'license': 'Apache-2.0', 'n_tokens_mistral': 479, 'n_tokens_neox': 443, 'n_words': 164, 'path': '461.HammingDistance.ipynb', 'repo_name': 'charlesxu90/leetcode_py', 'size': '1416'}