diff --git a/bit_manipulation/binary_and_operator.rb b/bit_manipulation/binary_and_operator.rb new file mode 100644 index 00000000..4cc0f5a9 --- /dev/null +++ b/bit_manipulation/binary_and_operator.rb @@ -0,0 +1,23 @@ +def binary_and(x, y) + raise 'Input must only contain positive integers' if x < 0 or y < 0 + + "0b" + (x & y).to_s(2) +end + +begin + binary_and(-1, 0) +rescue => e + puts e.message +end +# Input must only contain positive integers + +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 diff --git a/bit_manipulation/binary_count_setbits.rb b/bit_manipulation/binary_count_setbits.rb new file mode 100644 index 00000000..4deab5ab --- /dev/null +++ b/bit_manipulation/binary_count_setbits.rb @@ -0,0 +1,26 @@ +def binary_count_setbits(x) + raise 'Input must be a positive integer' if x < 0 + + binary = x.to_s(2) + + binary.chars.map { |c| c.to_i }.reduce(:+) +end + +begin + binary_count_setbits(-1) +rescue => e + puts e.message +end +# Input must be a positive integer + +puts binary_count_setbits(0) +# 0 + +puts binary_count_setbits(1) +# 1 + +puts binary_count_setbits(1024) +# 1 + +puts binary_count_setbits(1023) +# 10 diff --git a/bit_manipulation/binary_count_trailing_zeroes.rb b/bit_manipulation/binary_count_trailing_zeroes.rb new file mode 100644 index 00000000..40eafada --- /dev/null +++ b/bit_manipulation/binary_count_trailing_zeroes.rb @@ -0,0 +1,36 @@ +def binary_count_trailing_zeroes(x) + raise 'Input must be a positive integer' if x < 0 + + binary = x.to_s(2) + + count = 0 + binary.chars.reverse_each do |char| + break if char == "1" + + count += 1 + end + + count +end + +begin + binary_count_trailing_zeroes(-1) +rescue => e + puts e.message +end +# Input must be a positive integer + +puts binary_count_trailing_zeroes(0) +# 1 + +puts binary_count_trailing_zeroes(1023) +# 0 + +puts binary_count_trailing_zeroes(1024) +# 10 + +puts binary_count_trailing_zeroes(54) +# 1 + +puts binary_count_trailing_zeroes(121024) +# 6 diff --git a/bit_manipulation/binary_or_operator.rb b/bit_manipulation/binary_or_operator.rb new file mode 100644 index 00000000..43758b7a --- /dev/null +++ b/bit_manipulation/binary_or_operator.rb @@ -0,0 +1,24 @@ +def binary_or(x, y) + raise 'Input must only contain positive integers' if x < 0 or y < 0 + + "0b" + (x | y).to_s(2) +end + +begin + binary_or(-1, 0) +rescue => e + puts e.message +end +# Input must only contain positive integers + +puts binary_or(1, 1) +# 0b1 +puts binary_or(0, 1) +# 0b1 +puts binary_or(1024, 1024) +# 0b10000000000 +puts binary_or(0, 1023) +# 0b1111111111 +puts binary_or(16, 58) +# 0b110010 + diff --git a/bit_manipulation/binary_xor_operator.rb b/bit_manipulation/binary_xor_operator.rb new file mode 100644 index 00000000..634e8df1 --- /dev/null +++ b/bit_manipulation/binary_xor_operator.rb @@ -0,0 +1,44 @@ +def binary_xor(x, y) + raise 'Input must only contain positive integers' if x < 0 or y < 0 + + binary_x = x.to_s(2) + binary_y = y.to_s(2) + + if binary_x.length > binary_y.length + prefix = "0" * (binary_x.length - binary_y.length) + binary_y = prefix + binary_y + elsif binary_y.length > binary_x.length + prefix = "0" * (binary_y.length - binary_x.length) + binary_x = prefix + binary_x + end + result = "0b" + binary_x.each_char.with_index do |x_char, i| + y_char = binary_y[i] + + if (x_char == "1" && y_char != "1") || (x_char != "1" && y_char == "1") + result += "1" + else + result += "0" + end + end + + result +end + +begin + binary_xor(-1, 0) +rescue => e + puts e.message +end +# Input must only contain positive integers + +puts binary_xor(1, 1) +# 0b0 +puts binary_xor(0, 1) +# 0b1 +puts binary_xor(1024, 1024) +# 0b00000000000 +puts binary_xor(0, 1023) +# 0b1111111111 +puts binary_xor(16, 58) +# 0b101010 diff --git a/bit_manipulation/single_bit_binary_operations.rb b/bit_manipulation/single_bit_binary_operations.rb new file mode 100644 index 00000000..b7da58ed --- /dev/null +++ b/bit_manipulation/single_bit_binary_operations.rb @@ -0,0 +1,100 @@ +def set_bit(x, position) + raise "position must be >= 0" if position < 0 + + x | (1 << position) +end + +puts set_bit(0, 0) +# 1 + +puts set_bit(0, 4) +# 16 + +puts set_bit(8, 3) +# 8 + +puts set_bit(8, 4) +# 24 + +begin + puts set_bit(8, -4) +rescue => e + puts e.message +end +# position must be >= 0 + + +def clear_bit(x, position) + raise "position must be > 0" if position < 0 + + x & ~(1 << position) +end + +puts clear_bit(0, 0) +# 0 + +puts clear_bit(0, 4) +# 0 + +puts clear_bit(8, 3) +# 0 + +puts clear_bit(24, 4) +# 8 + +begin + puts clear_bit(0, -4) +rescue => e + puts e.message +end +# position must be > 0 + +def flip_bit(x, position) + raise "position must be > 0" if position < 0 + + x ^ (1 << position) +end + +puts flip_bit(0, 0) +# 1 + +puts flip_bit(0, 4) +# 16 + +puts flip_bit(8, 3) +# 0 + +puts flip_bit(24, 4) +# 8 + +begin + puts flip_bit(0, -4) +rescue => e + puts e.message +end +# position must be > 0 + +def is_bit_set(x, position) + raise "position must be > 0" if position < 0 + + ((x >> position) & 1) == 1 +end + +puts is_bit_set(0, 0) +# false + +puts is_bit_set(1, 0) +# true + +puts is_bit_set(8, 3) +# true + +puts is_bit_set(24, 4) +# true + +begin + puts is_bit_set(0, -4) +rescue => e + puts e.message +end +# position must be > 0