Count how many 1s a number has in binary


Count how many 1s a number has in binary


the code

def count_yi(num):
	if num == 0:
	    return 0
	else:
	    return count_yi(num & num - 1) + 1
if __name__ == '__main__':
	print(count_yi(127))

The final result is: 7

Guess you like

Origin blog.csdn.net/a_13572035650/article/details/128051700