Skip to content

Commit fb50988

Browse files
committed
bitwise operators using 32-bit signed integer arithmetics
1 parent f581afa commit fb50988

File tree

2 files changed

+280
-0
lines changed

2 files changed

+280
-0
lines changed

test-data/bitwise.expected

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
bitwise and -1804289383 1681692777 1957747793 -719885386 596516649 1025202362 783368690 -2044897763
2+
-1804289383 -1804289383 70555657 338728977 -1810617712 268809 336599192 70254736 -2079059431
3+
1681692777 70555657 1681692777 1680906305 1142163488 537663529 605558824 607125600 68947977
4+
1957747793 338728977 1680906305 1957747793 1410353168 545266689 873486352 615530576 68178961
5+
-719885386 -1810617712 1142163488 1410353168 -719885386 17173280 353585330 68239794 -2078981612
6+
596516649 268809 537663529 545266689 17173280 596516649 554309672 578814240 34346505
7+
1025202362 336599192 605558824 873486352 353585330 554309672 1025202362 739328178 68767768
8+
783368690 70254736 607125600 615530576 68239794 578814240 739328178 783368690 101793808
9+
-2044897763 -2079059431 68947977 68178961 -2078981612 34346505 68767768 101793808 -2044897763
10+
11+
bitwise or -1804289383 1681692777 1957747793 -719885386 596516649 1025202362 783368690 -2044897763
12+
-1804289383 -1804289383 -193152263 -185270567 -713557057 -1208041543 -1115686213 -1091175429 -1770127715
13+
1681692777 -193152263 1681692777 1958534265 -180356097 1740545897 2101336315 1857935867 -432152963
14+
1957747793 -185270567 1958534265 1957747793 -172490761 2008997753 2109463803 2125585907 -155328931
15+
-719885386 -713557057 -180356097 -172490761 -719885386 -140542017 -48268354 -4756490 -685801537
16+
596516649 -1208041543 1740545897 2008997753 -140542017 596516649 1067409339 801071099 -1482727619
17+
1025202362 -1115686213 2101336315 2109463803 -48268354 1067409339 1025202362 1069242874 -1088463169
18+
783368690 -1091175429 1857935867 2125585907 -4756490 801071099 1069242874 783368690 -1363322881
19+
-2044897763 -1770127715 -432152963 -155328931 -685801537 -1482727619 -1088463169 -1363322881 -2044897763
20+
21+
bitwise xor -1804289383 1681692777 1957747793 -719885386 596516649 1025202362 783368690 -2044897763
22+
-1804289383 0 -263707920 -523999544 1097060655 -1208310352 -1452285405 -1161430165 308931716
23+
1681692777 -263707920 0 277627960 -1322519585 1202882368 1495777491 1250810267 -501100940
24+
1957747793 -523999544 277627960 0 -1582843929 1463731064 1235977451 1510055331 -223507892
25+
-719885386 1097060655 -1322519585 -1582843929 0 -157715297 -401853684 -72996284 1393180075
26+
596516649 -1208310352 1202882368 1463731064 -157715297 0 513099667 222256859 -1517074124
27+
1025202362 -1452285405 1495777491 1235977451 -401853684 513099667 0 329914696 -1157230937
28+
783368690 -1161430165 1250810267 1510055331 -72996284 222256859 329914696 0 -1465116689
29+
-2044897763 308931716 -501100940 -223507892 1393180075 -1517074124 -1157230937 -1465116689 0
30+
31+
-1804289383 1681692777 1957747793 -719885386 596516649 1025202362 783368690 -2044897763
32+
bitwise not 1804289382 -1681692778 -1957747794 719885385 -596516650 -1025202363 -783368691 2044897762

test-data/bitwise.wend

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
fun main() {
2+
var i:int; var j:int;
3+
4+
// _ _ _____
5+
// /\ | \ | | __ \
6+
// / \ | \| | | | |
7+
// / /\ \ | . ` | | | |
8+
// / ____ \| |\ | |__| |
9+
// /_/ \_\_| \_|_____/
10+
11+
fun and(a:int, b:int) : int {
12+
var result:int;
13+
var pow:int;
14+
15+
result = 0;
16+
if (a<0 && b<0) {
17+
result = -2147483648;
18+
}
19+
if (a<0) {
20+
a = a + 2147483648;
21+
}
22+
if (b<0) {
23+
b = b + 2147483648;
24+
}
25+
pow = 1;
26+
while a>0 || b>0 {
27+
if a % 2 == 1 && b % 2 == 1 {
28+
result = result + pow;
29+
}
30+
a = a / 2;
31+
b = b / 2;
32+
pow = pow * 2;
33+
}
34+
return result;
35+
}
36+
37+
// __ ______ _____
38+
// \ \ / / __ \| __ \
39+
// \ V / | | | |__) |
40+
// > <| | | | _ /
41+
// / . \ |__| | | \ \
42+
// /_/ \_\____/|_| \_\
43+
44+
fun xor(a:int, b:int) : int {
45+
return a - and(a,b) + b - and(a,b);
46+
}
47+
48+
// ____ _____
49+
// / __ \| __ \
50+
// | | | | |__) |
51+
// | | | | _ /
52+
// | |__| | | \ \
53+
// \____/|_| \_\
54+
55+
fun or(a:int, b:int) : int {
56+
return xor(xor(a,b),and(a,b));
57+
}
58+
59+
// _ _ ____ _______
60+
// | \ | |/ __ \__ __|
61+
// | \| | | | | | |
62+
// | . ` | | | | | |
63+
// | |\ | |__| | | |
64+
// |_| \_|\____/ |_|
65+
66+
fun not(a:int) : int {
67+
return -1 - a;
68+
}
69+
70+
// _ _ _ _
71+
// | | | | (_) | |
72+
// _ __ _ __ ___| |_| |_ _ _ _ __ _ __ _ _ __ | |_
73+
// | '_ \| '__/ _ \ __| __| | | | | '_ \| '__| | '_ \| __|
74+
// | |_) | | | __/ |_| |_| |_| | | |_) | | | | | | | |_
75+
// | .__/|_| \___|\__|\__|\__, | | .__/|_| |_|_| |_|\__|
76+
// | | __/ | | |
77+
// |_| |___/ |_|
78+
79+
fun padprint(a:int) {
80+
var n:int;
81+
var nspaces:int;
82+
83+
n = a;
84+
nspaces = 16;
85+
if (n<=0) {
86+
nspaces = nspaces - 1;
87+
}
88+
while n!=0 {
89+
n = n / 10;
90+
nspaces = nspaces - 1;
91+
}
92+
n = 0;
93+
while n<nspaces {
94+
print " ";
95+
n = n + 1;
96+
}
97+
print a;
98+
}
99+
100+
// ___ _ _
101+
// / _ \ | | | |
102+
// | (_) | | |_ ___ ___| |_ ___ __ _ ___ ___ ___
103+
// > _ < | __/ _ \/ __| __| / __/ _` / __|/ _ \/ __|
104+
// | (_) | | || __/\__ \ |_ | (_| (_| \__ \ __/\__ \
105+
// \___/ \__\___||___/\__| \___\__,_|___/\___||___/
106+
107+
fun test(i:int) : int {
108+
if (i<4) {
109+
if (i<2) {
110+
if (i<1) {
111+
return -1804289383;
112+
} else {
113+
return 1681692777;
114+
}
115+
} else {
116+
if (i<3) {
117+
return 1957747793;
118+
} else {
119+
return -719885386;
120+
}
121+
}
122+
} else {
123+
if (i<6) {
124+
if (i<5) {
125+
return 596516649;
126+
} else {
127+
return 1025202362;
128+
}
129+
} else {
130+
if (i<7) {
131+
return 783368690;
132+
} else {
133+
return -2044897763;
134+
}
135+
}
136+
}
137+
}
138+
139+
// _ _ _ _ _
140+
// | | | | | | | |
141+
// _ __ _ _ _ __ | |_ ___ ___| |_ ___| | | |
142+
// | '__| | | | '_ \ | __/ _ \/ __| __/ __| | | |
143+
// | | | |_| | | | | | || __/\__ \ |_\__ \_|_|_|
144+
// |_| \__,_|_| |_| \__\___||___/\__|___(_|_|_)
145+
146+
// _ _ _____
147+
// /\ | \ | | __ \
148+
// / \ | \| | | | |
149+
// / /\ \ | . ` | | | |
150+
// / ____ \| |\ | |__| |
151+
// /_/ \_\_| \_|_____/
152+
153+
print " bitwise and";
154+
i = 0;
155+
while i<8 {
156+
padprint(test(i));
157+
i = i + 1;
158+
}
159+
print "\n";
160+
i = 0;
161+
while i<8 {
162+
padprint(test(i));
163+
j = 0;
164+
while j<8 {
165+
padprint(and(test(i), test(j)));
166+
j = j + 1;
167+
}
168+
print "\n";
169+
i = i + 1;
170+
}
171+
print "\n";
172+
173+
// ____ _____
174+
// / __ \| __ \
175+
// | | | | |__) |
176+
// | | | | _ /
177+
// | |__| | | \ \
178+
// \____/|_| \_\
179+
180+
print " bitwise or";
181+
i = 0;
182+
while i<8 {
183+
padprint(test(i));
184+
i = i + 1;
185+
}
186+
print "\n";
187+
i = 0;
188+
while i<8 {
189+
padprint(test(i));
190+
j = 0;
191+
while j<8 {
192+
padprint(or(test(i), test(j)));
193+
j = j + 1;
194+
}
195+
print "\n";
196+
i = i + 1;
197+
}
198+
print "\n";
199+
200+
// __ ______ _____
201+
// \ \ / / __ \| __ \
202+
// \ V / | | | |__) |
203+
// > <| | | | _ /
204+
// / . \ |__| | | \ \
205+
// /_/ \_\____/|_| \_\
206+
207+
print " bitwise xor";
208+
i = 0;
209+
while i<8 {
210+
padprint(test(i));
211+
i = i + 1;
212+
}
213+
print "\n";
214+
i = 0;
215+
while i<8 {
216+
padprint(test(i));
217+
j = 0;
218+
while j<8 {
219+
padprint(xor(test(i), test(j)));
220+
j = j + 1;
221+
}
222+
print "\n";
223+
i = i + 1;
224+
}
225+
print "\n";
226+
227+
// _ _ ____ _______
228+
// | \ | |/ __ \__ __|
229+
// | \| | | | | | |
230+
// | . ` | | | | | |
231+
// | |\ | |__| | | |
232+
// |_| \_|\____/ |_|
233+
234+
print " ";
235+
i = 0;
236+
while i<8 {
237+
padprint(test(i));
238+
i = i + 1;
239+
}
240+
print "\n bitwise not";
241+
j = 0;
242+
while j<8 {
243+
padprint(not(test(j)));
244+
j = j + 1;
245+
}
246+
print "\n";
247+
}
248+

0 commit comments

Comments
 (0)