Created
January 3, 2020 04:10
-
-
Save dedeco/3d11e2d9df5335ac37eb37e57033191a to your computer and use it in GitHub Desktop.
Como contar o número do bits 1 para um inteiro (não negativo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def count_bits(x): | |
qty_ones = 0 | |
while x: | |
qty_ones += x & 1 | |
x >>= 1 | |
return qty_ones | |
if __name__ == "__main__": | |
print(count_bits(13)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment