forked from psounis/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise08.py
More file actions
73 lines (58 loc) · 1.74 KB
/
Copy pathexercise08.py
File metadata and controls
73 lines (58 loc) · 1.74 KB
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
63
64
65
66
67
68
69
70
71
72
73
class Byte:
def __init__(self, s = ""):
if s == "":
self.array = [0 for i in range(8)]
else:
self.array = [int(c) for c in s]
def __str__(self):
st = [str(c) for c in self.array]
return "".join(st)
def __lshift__(self, other):
for i in range(other):
self.array.pop(0)
self.array.append(0)
def __rshift__(self, other):
for i in range(other):
self.array.pop()
self.array.insert(0,0)
def __and__(self, other):
new_byte = Byte("")
for i in range(8):
new_byte.array[i] = self.array[i] & other.array[i]
return new_byte
def __or__(self, other):
new_byte = Byte("")
for i in range(8):
new_byte.array[i] = self.array[i] | other.array[i]
return new_byte
def __xor__(self, other):
new_byte = Byte("")
for i in range(8):
new_byte.array[i] = self.array[i] ^ other.array[i]
return new_byte
def __invert__(self):
new_byte = Byte("")
for i in range(8):
new_byte.array[i] = 1 if self.array[i]==0 else 0
return new_byte
def __len__(self):
return 8
def __getitem__(self, item):
return self.array[item]
def __setitem__(self, key, value):
self.array[key] = value
b = Byte()
b2 = Byte("00010011")
print(b, b2)
b2 >> 2
print(b2)
b2 = Byte("00010011")
b3 = Byte("00110101")
print(f"\n{b2}\n{b3}(&)\n{'-'*8}\n{b2&b3}")
print(f"\n{b2}\n{b3}(|)\n{'-'*8}\n{b2|b3}")
print(f"\n{b2}\n{b3}(^)\n{'-'*8}\n{b2^b3}")
print(f"\n{b3}(~)\n{'-'*8}\n{~b3}")
for bit in b3:
print(bit)
b3[0] = 1
print(b3)