Skip to content

Commit c78639d

Browse files
committed
add comments on output for the error cases
1 parent d7f266a commit c78639d

6 files changed

Lines changed: 35 additions & 1 deletion

bit_manipulation/binary_and_operator.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
def binary_and(x, y)
2-
raise 'Input must only contain positive integers' if x < 0 or x < 0
2+
raise 'Input must only contain positive integers' if x < 0 or y < 0
33

44
"0b" + (x & y).to_s(2)
55
end
@@ -9,6 +9,7 @@ def binary_and(x, y)
99
rescue => e
1010
puts e.message
1111
end
12+
# Input must only contain positive integers
1213

1314
puts binary_and(1, 1)
1415
# 0b1

bit_manipulation/binary_count_setbits.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def binary_count_setbits(x)
1111
rescue => e
1212
puts e.message
1313
end
14+
# Input must be a positive integer
1415

1516
puts binary_count_setbits(0)
1617
# 0

bit_manipulation/binary_count_trailing_zeroes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def binary_count_trailing_zeroes(x)
1818
rescue => e
1919
puts e.message
2020
end
21+
# Input must be a positive integer
2122

2223
puts binary_count_trailing_zeroes(0)
2324
# 1

bit_manipulation/binary_or_operator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def binary_or(x, y)
99
rescue => e
1010
puts e.message
1111
end
12+
# Input must only contain positive integers
1213

1314
puts binary_or(1, 1)
1415
# 0b1

bit_manipulation/binary_xor_operator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def binary_xor(x, y)
3030
rescue => e
3131
puts e.message
3232
end
33+
# Input must only contain positive integers
3334

3435
puts binary_xor(1, 1)
3536
# 0b0

bit_manipulation/single_bit_binary_operations.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ def set_bit(x, position)
1616
puts set_bit(8, 4)
1717
# 24
1818

19+
begin
20+
puts set_bit(8, -4)
21+
rescue => e
22+
puts e.message
23+
end
24+
# position must be >= 0
25+
26+
1927
def clear_bit(x, position)
2028
raise "position must be > 0" if position < 0
2129

@@ -34,6 +42,13 @@ def clear_bit(x, position)
3442
puts clear_bit(24, 4)
3543
# 8
3644

45+
begin
46+
puts clear_bit(0, -4)
47+
rescue => e
48+
puts e.message
49+
end
50+
# position must be > 0
51+
3752
def flip_bit(x, position)
3853
raise "position must be > 0" if position < 0
3954

@@ -52,6 +67,13 @@ def flip_bit(x, position)
5267
puts flip_bit(24, 4)
5368
# 8
5469

70+
begin
71+
puts flip_bit(0, -4)
72+
rescue => e
73+
puts e.message
74+
end
75+
# position must be > 0
76+
5577
def is_bit_set(x, position)
5678
raise "position must be > 0" if position < 0
5779

@@ -69,3 +91,10 @@ def is_bit_set(x, position)
6991

7092
puts is_bit_set(24, 4)
7193
# true
94+
95+
begin
96+
puts is_bit_set(0, -4)
97+
rescue => e
98+
puts e.message
99+
end
100+
# position must be > 0

0 commit comments

Comments
 (0)