ハミング重み / Python

1の個数を数える。

def hamming_weight(val):
    # bin()で、2進数文字列に変換して、
    # その文字列のなかの'1'の個数を数える。
    return bin(val)[2:].count('1')

print(hamming_weight(0b11110011))

f:id:ti-nspire:20210407085439p:plain