forked from psounis/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise03.py
More file actions
51 lines (42 loc) · 1.3 KB
/
exercise03.py
File metadata and controls
51 lines (42 loc) · 1.3 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
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
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}")