-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBitHandling.py
32 lines (25 loc) · 948 Bytes
/
BitHandling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import logging
class BitHandling:
def __init__(self):
self.logger = logging.getLogger('ws28xx.BitHandling')
self.logger.debug("")
# testBit() returns a nonzero result, 2**offset, if the bit at 'offset' is one.
def testBit(self,int_type, offset):
mask = 1 << offset
return(int_type & mask)
# setBit() returns an integer with the bit at 'offset' set to 1.
def setBit(self,int_type, offset):
mask = 1 << offset
return(int_type | mask)
# setBitVal() returns an integer with the bit at 'offset' set to 1.
def setBitVal(self,int_type, offset, val):
mask = val << offset
return(int_type | mask)
# clearBit() returns an integer with the bit at 'offset' cleared.
def clearBit(self,int_type, offset):
mask = ~(1 << offset)
return(int_type & mask)
# toggleBit() returns an integer with the bit at 'offset' inverted, 0 -> 1 and 1 -> 0.
def toggleBit(self,int_type, offset):
mask = 1 << offset
return(int_type ^ mask)