Month: September 2009

Recent Posts
Uncategorized

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/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)