-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics.py
More file actions
33 lines (27 loc) · 790 Bytes
/
basics.py
File metadata and controls
33 lines (27 loc) · 790 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
27
28
29
30
31
32
33
class bit32:
def __init__(self, hexval, bits=32):
assert bits%4 == 0
self.val = hexval
self.bitwidth = bits
self.hexwidth = int(bits/4)
def __repr__(self):
return "{}bits:{}|0x{}|{}|".format(self.bitwidth, self.int, self.hexstring, self.bitstring)
@property
def int(self):
return(self.val)
@property
def bitstring(self):
return '{val:0{width}b}'.format(width=self.bitwidth, val=self.val)
@property
def hexstring(self):
return '{val:0{width}x}'.format(width=self.hexwidth, val=self.val)
# return f"{self.val:016x}"
@property
def bits(self):
return bin(self.val)
a = bit32(22, 32)
a = bit32(22, 16)
# a.hex = "asdf"
print(a)
# print(a.bitstring)
# print(a.hexstring)