-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathbit_operations_i64.py
121 lines (99 loc) · 2.21 KB
/
bit_operations_i64.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
from lpython import i64
def test_bitnot():
x: i64 = i64(123)
y: i64 = i64(456)
z: i64 = i64(115292150460684)
w: i64 = i64(-115292150460685)
p: i64 = ~x
q: i64 = ~y
r: i64 = ~z
s: i64 = ~w
print(p)
print(q)
print(r)
print(s)
assert(p == i64(-124))
assert(q == i64(-457))
assert(r == i64(-115292150460685))
assert(s == i64(115292150460684))
def test_bitand():
x: i64 = i64(741)
y: i64 = i64(982)
z: i64 = i64(115292150460684)
w: i64 = i64(-115292150460685)
p: i64 = x & y
q: i64 = y & z
r: i64 = z & w
s: i64 = x & w
print(p)
print(q)
print(r)
print(s)
assert(p == i64(708))
assert(q == i64(260))
assert(r == i64(0))
assert(s == i64(737))
def test_bitor():
x: i64 = i64(741)
y: i64 = i64(982)
z: i64 = i64(115292150460684)
w: i64 = i64(-115292150460685)
p: i64 = x | y
q: i64 = y | z
r: i64 = z | w
s: i64 = x | w
print(p)
print(q)
print(r)
print(s)
assert(p == i64(1015))
assert(q == i64(115292150461406))
assert(r == i64(-1))
assert(s == i64(-115292150460681))
def test_bitxor():
x: i64 = i64(741)
y: i64 = i64(982)
z: i64 = i64(115292150460684)
w: i64 = i64(-115292150460685)
p: i64 = x ^ y
q: i64 = y ^ z
r: i64 = z ^ w
s: i64 = x ^ w
print(p)
print(q)
print(r)
print(s)
assert(p == i64(307))
assert(q == i64(115292150461146))
assert(r == i64(-1))
assert(s == i64(-115292150461418))
def test_left_shift():
a: i64 = i64(4294967296)
shift_amount: i64 = i64(2)
b: i64 = a << shift_amount
print(b)
assert b == i64(17179869184)
a = i64(-4294967296)
shift_amount = i64(2)
b = a << shift_amount
print(b)
assert b == i64(-17179869184)
def test_right_shift():
a: i64 = i64(4294967296)
shift_amount: i64 = i64(2)
b: i64 = a >> shift_amount
print(b)
assert b == i64(1073741824)
a = i64(-4294967296)
shift_amount = i64(2)
b = a >> shift_amount
print(b)
assert b == i64(-1073741824)
def main0():
test_bitnot()
test_bitand()
test_bitor()
test_bitxor()
test_left_shift()
test_right_shift()
main0()