Counting non-zero bits in a positive number with a Python one-liner

Counting non-zero bits in a positive number with a Python one-liner

cooowde

3 года назад

48 Просмотров

In this example we convert a positive number to binary using bin() function and then count all the 1s in the obtained string. bin() function returns a binary representation in a format 0b101 for a number 5, therefore we cut the "0b" part using [2:] syntax.

#python #numbers #binary
Ссылки и html тэги не поддерживаются


Комментарии: