forked from in3rsha/sha256-animation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.rb
47 lines (43 loc) · 1.07 KB
/
add.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
# -----
# Input
# -----
# defaults
numbers =
[0b01000000000000001111111111111111,
0b01000000000000000000000000000000,
0b01000000000000000000000000000000,
0b01000000000000000000000000000000,
0b01000000000000000000000000000000,
]
# arguments passed
if ARGV.size >= 2
numbers = ARGV.map {|x| x.to_i(2)} # convert binary strings to integers
end
# ---------
# Animation
# ---------
total = numbers.inject(:+)
width = total.to_s(2).size
(numbers.size + 1).times do |i|
system "clear"
numbers.each.with_index do |x, j|
print ("%032b" % x).rjust(width, " ")
if j > 0
if j <= i
print " +"
end
end
print "\n"
end
puts "--------------------------------".rjust(width, " ")
if i == 0
puts "" # numbers[0].to_s(2).rjust(32, "0").rjust(width, " ")
elsif i < numbers.size # for each addition
puts numbers[0..i].inject(:+).to_s(2).rjust(32, "0").rjust(width, " ")
sleep 0.4 if i == numbers.size-1 # slight delay before the modulus function
else
puts (numbers[0..i].inject(:+) % 2**32).to_s(2).rjust(32, "0").rjust(width, " ") + " mod 2**32"
end
sleep 0.4
end
sleep 1.0