0
number of ones
maybe it help you friend
5 Réponses
+ 1
decimal_number = int(input())
binary_number = bin(decimal_number)
kick_prefix = binary_number[2:]
build_list = list(kick_prefix)
a = 0
for i in build_list:
if i == '1':
a += 1
else:
continue
print(a)
+ 1
a=kick_prefix.count("1")
0
x = int(input())
y=''
while True:
y += str(x % 2)
x = x // 2
if x // 2 == 0:
break
y= y+ '1'
print(y.count('1'))
0
print(str(bin(int(input()))).count('1'))
The long version:
decimal_number = int(input())
binary_number = bin(decimal_number)
binary_string = str(binary_number)
number_of_ones = binary_string.count('1')
print(number_of_ones)
Explanation:
-Turn the given integer into a binary number. E.g.
24 -> 11000
-Turn the binary number into a string. E.g.
11000 -> "11000"
-Use the .count() function to count how many times the "1" character appears in the string
0
num = format(int(input()), "b")
it = list(num)
count = 0
for let in it:
if let == "1":
count += 1
print(count)