Package lacewing :: Module byteflag
[frames] | no frames]

Source Code for Module lacewing.byteflag

 1  # Copyright (c) 2011 Mathias Kaerlev. 
 2  # See LICENSE for details. 
 3   
 4  """ 
 5  Perfom flag operations on numbers. 
 6  """ 
 7   
8 -def listFlag(flaglist):
9 """ 10 Takes a list of bools and returns 11 a flagbyte. 12 """ 13 flag = 0 14 for index, item in enumerate(flaglist): 15 flag = setFlag(flag, index, item) 16 return flag
17
18 -def setFlag(flagbyte, pos, status):
19 """ 20 Sets the bit at 'pos' to 'status', and 21 returns the modified flagbyte. 22 """ 23 if status: 24 return flagbyte | 2**pos 25 else: 26 return flagbyte & ~2**pos
27
28 -def getFlag(flagbyte, pos):
29 """ 30 Returns the bit at 'pos' in 'flagbyte' 31 """ 32 mask = 2**pos 33 result = flagbyte & mask 34 return (result == mask)
35
36 -def getFlags(flagbyte, *positions):
37 """ 38 Returns the bits specified in the arguments 39 """ 40 return [getFlag(flagbyte, pos) for pos in positions]
41