Skip to content

Instantly share code, notes, and snippets.

@NuarkNoir
Created January 22, 2019 03:44
Show Gist options
  • Save NuarkNoir/e54a967b8f5db346713100cf207e85cb to your computer and use it in GitHub Desktop.
Save NuarkNoir/e54a967b8f5db346713100cf207e85cb to your computer and use it in GitHub Desktop.
Count zeros and ones in decimals binary representation
def dec2bin(dec):
return str(bin(dec))[2:]
def cnt_ones(num, zeros=False):
binform = dec2bin(num)
vtc = "1"
if zeros:
vtc = "0"
return sum([1 for i in binform if i == vtc])
var = 4**2016+2**2018-6
print("bin:", dec2bin(var))
print("1:", cnt_ones(var))
print("0:", cnt_ones(var, True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment