-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitwise.spwn
103 lines (99 loc) · 2.71 KB
/
bitwise.spwn
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
pad_with_zero = (item1: @string, item2: @string) {
max_length = $.max(item1.length, item2.length)
return [
"0" * (max_length-item1.length) + item1,
"0" * (max_length-item2.length) + item2
]
}
type @bw
impl @bw {
and: (op1: @string, op2: @string) {
let result = pad_with_zero(op1, op2)
item1 = result[0]
item2 = result[1]
let output = ""
for i in ..item1.length {
if item1[i] == "1" && item2[i] == "1" { output += "1" }
else { output += "0" }
}
return output
},
or: (op1: @string, op2: @string) {
let result = pad_with_zero(op1, op2)
item1 = result[0]
item2 = result[1]
let output = ""
for i in ..item1.length {
if item1[i] == "1" || item2[i] == "1" { output += "1" }
else { output += "0" }
}
return output
},
xor: (op1: @string, op2: @string) {
let result = pad_with_zero(op1, op2)
item1 = result[0]
item2 = result[1]
let output = ""
for i in ..item1.length {
if (item1[i] == "1" && item2[i] == "0") || (item1[i] == "0" && item2[i] == "1") { output += "1" }
else { output += "0" }
}
return output
},
not: (op1: @string) {
let output = ""
for i in ..op1.length {
if op1[i] == "1" { output += "0" }
else { output += "1" }
}
return output
}
}
to_arr = (str: @string) {
return str.split("")
}
impl @bw {
lshift_once: (op1: @string) {
return op1[1:]+"0"
},
lshift: (op1: @string, amount: @number = 1) {
let current = op1
for i in ..amount {
current = @bw::lshift_once(current)
}
return current
},
rshift_once: (op1: @string) {
return "0"+op1[:-1]
},
rshift: (op1: @string, amount: @number = 1) {
let current = op1
for i in ..amount {
current = @bw::rshift_once(current)
}
return current
},
lrotate_once: (op1: @string) {
let modded = "".join(to_arr(@bw::lshift_once(op1))[:-1])
return modded + op1[0]
},
lrotate: (op1: @string, amount: @number = 1) {
let current = op1
for i in ..amount {
current = @bw::lrotate_once(current)
}
return current
},
rrotate_once: (op1: @string) {
let modded = "".join(to_arr(@bw::rshift_once(op1))[1:])
return op1[op1.length-1] + modded
},
rrotate: (op1: @string, amount: @number = 1) {
let current = op1
for i in ..amount {
current = @bw::rrotate_once(current)
}
return current
}
}
return @bw