Reverse Bitwise Flag Calculator

Sometimes when you need to deal with bitwise flags, it can get a little difficult to figure out what that gigantic number you’re using really consists of. The below should help:

#!/usr/bin/python
#bc.py - ./bc.py 108 = flags that make up 108.
import sys

def decompose(i):
  limit = i+1
  tmpi = 1
  while tmpi < limit:
    if i & tmpi == tmpi:
      print tmpi
    tmpi *= 2

if __name__ == "__main__":
  try:
    b = int(sys.argv[1])
    decompose(b)
  except:
    print "You need to specify an integer"
    exit(0)