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

Source Code for Module lacewing.byteflag

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