-
Notifications
You must be signed in to change notification settings - Fork 0
/
Address.py
62 lines (51 loc) · 1.75 KB
/
Address.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
class AddressN:
def __init__(self, addr, n):
tag = '{:0' + str(n) + 'b}'
if type(addr) == int:
self.intformat = addr
self.bitformat = tag.format(np.uint32(addr))
elif type(addr) == str:
self.intformat = self.bitstoint(addr)
self.bitformat = tag.format(np.uint32(self.intformat))
def bitstoint(self, bits):
eq = 0
m = 1
for bit in bits[::-1]:
b = int(bit)
eq += b * m
m *= 2
return eq
def __add__(self, other):
if type(other) == int:
return AddressN(self.intformat + other)
elif type(other) == AddressN:
return AddressN(self.intformat + AddressN.intformat)
def __str__(self):
return str(self.intformat) + ':' + self.bitformat
class Address32:
def __init__(self, addr):
if type(addr) == int:
self.intformat = addr
self.bitformat = '{:032b}'.format(np.uint32(addr))
# elif len(addr) != 32:
# raise Exception('Your address should be 32 bits', addr)
elif type(addr) == str:
self.intformat = self.bitstoint(addr)
self.bitformat = '{:032b}'.format(np.uint32(self.intformat))
def bitstoint(self, bits):
eq = 0
m = 1
for bit in bits[::-1]:
b = int(bit)
eq += b * m
m *= 2
return eq
def __add__(self, other):
if type(other) == int:
return Address32(self.intformat + other)
#Todo: fix this
elif type(other) == Address32:
return Address32(self.intformat + other.intformat)
def __str__(self):
return str(self.intformat) + ':' + self.bitformat