Skip to content

Instantly share code, notes, and snippets.

@dedeco
Created January 3, 2020 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dedeco/3d11e2d9df5335ac37eb37e57033191a to your computer and use it in GitHub Desktop.
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)
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