-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecial_ops.rb
23 lines (22 loc) · 1.12 KB
/
special_ops.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# & is the bitwise and operation. It operates on numbers in their binary representation.
# It takes a pair of bits at a given position in the input numbers and
# puts a 1 in that position in the output if they are both 1s. Otherwise
# it puts a 0.
def bitwise_and(x, y)
ans = x & y
# rjust(5) pads the string with spaces (on the left) up to a length of 5
puts x.to_s.rjust(5) + ' = ' + x.to_s(2).rjust(8, '0')
puts y.to_s.rjust(5) + ' = ' + y.to_s(2).rjust(8, '0')
puts (x.to_s + '&' + y.to_s).rjust(5) + ' = ' + ans.to_s(2).rjust(8, '0') + ' = ' + ans.to_s
end
# ^ is the bitwise xor operation. It operates on numbers in their binary representation.
# It takes a pair of bits at a given position in the input numbers and
# puts a 1 in that position in the output if exactly one is a 1. Otherwise
# it puts a 0.
def bitwise_xor(x, y)
ans = x ^ y
# rjust(5) pads the string with spaces (on the left) up to a length of 5
puts x.to_s.rjust(5) + ' = ' + x.to_s(2).rjust(8, '0')
puts y.to_s.rjust(5) + ' = ' + y.to_s(2).rjust(8, '0')
puts (x.to_s + '^' + y.to_s).rjust(5) + ' = ' + ans.to_s(2).rjust(8, '0') + ' = ' + ans.to_s
end