Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
XOR & Count bits (Brian Kernighan’s way) solution in Speedy category for The Hamming Distance by 1-more
def checkio(n, m):
xor= n^m
# Count bits (Brian Kernighan’s way)
count=0
while xor:
count+= 1
xor&= xor-1
return count
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(117, 17) == 3, "First example"
assert checkio(1, 2) == 2, "Second example"
assert checkio(16, 15) == 5, "Third example"
Feb. 21, 2018
Comments: