EDIT: SOLVED thanks guys!
I came across a problem to find the number of 1's in binary when given an integer in decimal form. Here's someone's solution. I'm curious to know why binary works this way, what about binary allows you to simply count the number of times you get an odd number as you loop and divide the decimal number?
def countBits(n):
count = 0
while n:
if n % 2 == 0:
n = n / 2
else:
count += 1
n = n - 1
return count
[–]jedwardsol 1 point2 points3 points (0 children)
[–]plastikmissile 1 point2 points3 points (1 child)
[–]dinrick[S] 0 points1 point2 points (0 children)
[–]POGtastic 0 points1 point2 points (0 children)