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)
This blog post was posted to orz on Monday September 21st, 2009 at 3:00PM
 

Leave a Comment

You can use bbcode in the comment: e.g. [b]This is bold[/b], [url]http://www.willmcgugan.com[/url], [code python]import this[/code]
Preview Posting...
Previewing comment, please wait a moment...
Search for Posts
Possibly related posts
Tags
Popular Tags
 
Archives
2010
 
Recent Comments
 
© 2008 Will McGugan.

A technoblog blog, design by Will McGugan