jeudi 27 mai 2010

Count the nb of bits in an integer

General idea: shr bits one by one and see if the dropped bit was 0 or 1

total_b = 0
x = y
while (x > 0)
x = y >> 1 # shift one bit to the right
b = 0
if (y - 2*x == 0)
b = 0
else
b = 1
if b = 1
total_b++

Aucun commentaire:

Enregistrer un commentaire