mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-30 22:24:11 +01:00
22 lines
357 B
Ruby
22 lines
357 B
Ruby
def binary_and(x, y)
|
|
raise 'Input must only contain positive integers' if x < 0 or x < 0
|
|
|
|
"0b" + (x & y).to_s(2)
|
|
end
|
|
|
|
begin
|
|
binary_and(-1, 0)
|
|
rescue => e
|
|
puts e.message
|
|
end
|
|
|
|
puts binary_and(1, 1)
|
|
# 0b1
|
|
puts binary_and(0, 1)
|
|
# 0b0
|
|
puts binary_and(1024, 1024)
|
|
# 0b10000000000
|
|
puts binary_and(0, 1023)
|
|
# 0b0000000000
|
|
puts binary_and(16, 58)
|
|
# 0b010000
|