-
Notifications
You must be signed in to change notification settings - Fork 165
/
xor.rb
192 lines (171 loc) · 4.2 KB
/
xor.rb
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -----
# Input
# -----
# just do a simple XOR if binary string arguments passed
if ARGV.size >= 2
width = ARGV.map(&:size).max # work out the longest binary string (for display purposes)
system "clear"
ARGV.each do |binary|
puts binary.rjust(width, " ")
end
puts "-" * width
puts ARGV.map{|x| x.to_i(2)}.reduce(0, :^).to_s(2).rjust(width, "0")
exit
end
# ---------
# Animation
# ---------
# If no arguments passed, show an animation that showcases the XOR operator across 3 inputs
off = 0b00000000000000000000000000000000
on = 0b11111111111111111111111111111111
split = 0b10101010101010101010101010101010
delay = 0.4
# Up To All 1
system "clear"
puts ("%032b" % off)
puts ("%032b" % off) + " XOR"
puts ("%032b" % off) + " XOR"
puts "--------------------------------"
puts "%032b" % (off ^ off ^ off)
sleep delay
system "clear"
puts ("%032b" % on)
puts ("%032b" % off) + " XOR"
puts ("%032b" % off) + " XOR"
puts "--------------------------------"
puts "%032b" % (on ^ off ^ off)
sleep delay
system "clear"
puts ("%032b" % on)
puts ("%032b" % on) + " XOR"
puts ("%032b" % off) + " XOR"
puts "--------------------------------"
puts "%032b" % (on ^ on ^ off)
sleep delay
system "clear"
puts ("%032b" % on)
puts ("%032b" % on) + " XOR"
puts ("%032b" % on) + " XOR"
puts "--------------------------------"
puts "%032b" % (on ^ on ^ on)
sleep delay
# Back To All 0
system "clear"
puts ("%032b" % off)
puts ("%032b" % on) + " XOR"
puts ("%032b" % on) + " XOR"
puts "--------------------------------"
puts "%032b" % (off ^ on ^ on)
sleep delay
system "clear"
puts ("%032b" % off)
puts ("%032b" % off) + " XOR"
puts ("%032b" % on) + " XOR"
puts "--------------------------------"
puts "%032b" % (off ^ off ^ on)
sleep delay
system "clear"
puts ("%032b" % off)
puts ("%032b" % off) + " XOR"
puts ("%032b" % off) + " XOR"
puts "--------------------------------"
puts "%032b" % (off ^ off ^ off)
sleep delay
# Only One 1
system "clear"
puts ("%032b" % on)
puts ("%032b" % off) + " XOR"
puts ("%032b" % off) + " XOR"
puts "--------------------------------"
puts "%032b" % (on ^ off ^ off)
sleep delay
system "clear"
puts ("%032b" % off)
puts ("%032b" % on) + " XOR"
puts ("%032b" % off) + " XOR"
puts "--------------------------------"
puts "%032b" % (off ^ on ^ off)
sleep delay
system "clear"
puts ("%032b" % off)
puts ("%032b" % off) + " XOR"
puts ("%032b" % on) + " XOR"
puts "--------------------------------"
puts "%032b" % (off ^ off ^ on)
sleep delay
# Majority 1
system "clear"
puts ("%032b" % off)
puts ("%032b" % on) + " XOR"
puts ("%032b" % on) + " XOR"
puts "--------------------------------"
puts "%032b" % (off ^ on ^ on)
sleep delay
system "clear"
puts ("%032b" % on)
puts ("%032b" % off) + " XOR"
puts ("%032b" % on) + " XOR"
puts "--------------------------------"
puts "%032b" % (on ^ off ^ on)
sleep delay
system "clear"
puts ("%032b" % on)
puts ("%032b" % on) + " XOR"
puts ("%032b" % off) + " XOR"
puts "--------------------------------"
puts "%032b" % (on ^ on ^ off)
sleep delay
# All 1
system "clear"
puts ("%032b" % on)
puts ("%032b" % on) + " XOR"
puts ("%032b" % on) + " XOR"
puts "--------------------------------"
puts "%032b" % (on ^ on ^ on)
sleep delay
sleep delay
# Split
system "clear"
puts ("%032b" % on)
puts ("%032b" % on) + " XOR"
puts ("%032b" % split) + " XOR"
puts "--------------------------------"
puts "%032b" % (on ^ on ^ split)
sleep delay
# Split - Rotate
def rotr(n, x)
mask = 2**32 - 1
right = (x >> n) & mask
left = (x << 32-n) & mask
result = right | left
end
9.times do |i|
system "clear"
puts ("%032b" % on)
puts ("%032b" % on) + " XOR"
puts ("%032b" % rotr(i, split)) + " XOR"
puts "--------------------------------"
puts "%032b" % (on ^ on ^ rotr(i, split))
sleep delay/2
end
sleep delay
9.times do |i|
system "clear"
puts ("%032b" % on)
puts ("%032b" % off) + " XOR"
puts ("%032b" % rotr(i, split)) + " XOR"
puts "--------------------------------"
puts "%032b" % (on ^ off ^ rotr(i, split))
sleep delay/2
end
sleep delay
9.times do |i|
system "clear"
puts ("%032b" % off)
puts ("%032b" % off) + " XOR"
puts ("%032b" % rotr(i, split)) + " XOR"
puts "--------------------------------"
puts "%032b" % (off ^ off ^ rotr(i, split))
sleep delay/2
end
sleep delay