fork(7) download
  1. def in_binary(n):
  2. if n == 0:
  3. return [] # base case
  4. else:
  5. digit = n & 0x1 # bit mask
  6. return in_binary(n >> 1) + [digit] # recursion
  7.  
  8. def from_binary(b):
  9. # add value of each digit, using enumerate to get its place
  10. # list comprehensions are Pythonic
  11. return sum([(2**place) * value
  12. for place, value in enumerate(b)])
  13.  
  14. two = in_binary(2)
  15. print from_binary(two + two)
Success #stdin #stdout 0.01s 7896KB
stdin
Standard input is empty
stdout
5