Hamming Distance
Python
Medium
2 views
Problem Description
Read two non-negative integers a and b. Hamming distance is number of different bits. Output it.
Input Format
One line: a b.
Output Format
One integer distance.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<2: sys.exit(0)
a=int(p[0]); b=int(p[1])
x=a^b
c=0
while x:
x&=x-1
c+=1
sys.stdout.write(str(c))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!