-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathbit_operations_i32.py
124 lines (99 loc) · 1.84 KB
/
bit_operations_i32.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from lpython import i32
def test_bitnot():
x: i32 = 5
y: i32 = 0
z: i32 = 2147483647
w: i32 = -2147483648
p: i32 = ~x
q: i32 = ~y
r: i32 = ~z
s: i32 = ~w
print(p)
print(q)
print(r)
print(s)
assert(p == -6)
assert(q == -1)
assert(r == -2147483648)
assert(s == 2147483647)
def test_bitand():
x: i32 = 5
y: i32 = 3
z: i32 = 2147483647
w: i32 = -2147483648
p: i32 = x & y
q: i32 = y & z
r: i32 = z & w
s: i32 = x & w
print(p)
print(q)
print(r)
print(s)
assert(p == 1)
assert(q == 3)
assert(r == 0)
assert(s == 0)
def test_bitor():
x: i32 = 5
y: i32 = 3
z: i32 = 2147483647
w: i32 = -2147483648
p: i32 = x | y
q: i32 = y | z
r: i32 = z | w
s: i32 = x | w
print(p)
print(q)
print(r)
print(s)
assert(p == 7)
assert(q == 2147483647)
assert(r == -1)
assert(s == -2147483643)
def test_bitxor():
x: i32 = 5
y: i32 = 3
z: i32 = 2147483647
w: i32 = -2147483648
p: i32 = x ^ y
q: i32 = y ^ z
r: i32 = z ^ w
s: i32 = x ^ w
print(p)
print(q)
print(r)
print(s)
assert(p == 6)
assert(q == 2147483644)
assert(r == -1)
assert(s == -2147483643)
def test_left_shift():
a: i32 = 4
shift_amount: i32 = 2
b: i32 = a << shift_amount
print(b)
assert b == 16
a = -16
shift_amount = 2
b = a << shift_amount
print(b)
assert b == -64
def test_right_shift():
a: i32 = 16
shift_amount: i32 = 2
b: i32 = a >> shift_amount
print(b)
assert b == 4
a = -16
shift_amount = 2
b = a >> shift_amount
print(b)
assert b == -4
def main0():
test_bitnot()
test_bitand()
test_bitor()
test_bitxor()
test_left_shift()
test_right_shift()
main0()