hamming.py

Created by alain-busser

Created on January 02, 2020

269 Bytes

compute the Hamming weight (number of “1” in the binary code of n) of an integer n, and the Hamming distance (number of bits which are not the same) between two integers a and b.


from kandinsky import *
blue = (0,0,255)
red = (255,0,0)

def weight(n):
  return bin(n).count('1')

def distance(a,b):
  return weight(a^b)

for a in range(128):
  for b in range(128):
    d = distance(a,b)
    c = (32*d,32*d,32*d)
    set_pixel(a,b,c)