-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitfields.py
More file actions
26 lines (25 loc) · 834 Bytes
/
bitfields.py
File metadata and controls
26 lines (25 loc) · 834 Bytes
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
class bitfield:
def __init__(self, field, off, size):
self.field = field
self.off = off
self.size = size
def __get__(self, inst, owner=None):
if inst is not None:
mask = ~(-1 << self.size)
val = getattr(inst, self.field)
return val >> self.off & mask
return self
def __set__(self, inst, value):
mask = ~(-1 << self.size)
value &= mask
val = getattr(inst, self.field)
val = val & ~(mask << self.off) | value << self.off
setattr(inst, self.field, val)
class Instruction:
def __init__(self, value):
self.value = value
def __repr__(self):
return 'Instruction({:#018b})'.format(self.value)
a = bitfield('value', 0, 4)
b = bitfield('value', 4, 4)
c = bitfield('value', 8, 8)