forked from kostis/ntua_compilers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntXor.dana
More file actions
65 lines (50 loc) · 1012 Bytes
/
IntXor.dana
File metadata and controls
65 lines (50 loc) · 1012 Bytes
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
# A (not so efficient) program that returns the XOR of 2 ints
def main
# A binary number is represented with an array
# where every bit is stored as an int
# Function definitions
def DecToBin: n as int, N as int[]
var max i is int
max := 16384
i := 14
loop:
N[i] := n / max
n := n % max
max := max / 2
i := i - 1
if i < 0: break
def BinToDec is int: N as int[]
var max i n is int
max := 16384
i := 14
n := 0
loop:
n := n + N[i] * max
max := max / 2
i := i - 1
if i < 0: break
return : n
def XOR: A B X as int[]
var i is int
i := 14
loop:
X[i] := (A[i] + B[i]) % 2
i := i - 1
if i < 0: break
# Main program
var a b x is int
var A B X is int[15]
# Read input
writeString: "Input 2 non negative integers"
a := readInteger()
b := readInteger()
# Convert to binary
DecToBin: a, A
DecToBin: b, B
# Calculate XOR
XOR: A, B, X
# Convert result to int
x := BinToDec(X)
# Print result
writeInteger: x
writeString : "\n"