Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bit_manipulation algorithms #139

Merged
merged 2 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions bit_manipulation/binary_and_operator.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions bit_manipulation/binary_count_setbits.rb
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions bit_manipulation/binary_count_trailing_zeroes.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions bit_manipulation/binary_or_operator.rb
Original file line number Diff line number Diff line change
@@ -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

44 changes: 44 additions & 0 deletions bit_manipulation/binary_xor_operator.rb
Original file line number Diff line number Diff line change
@@ -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
100 changes: 100 additions & 0 deletions bit_manipulation/single_bit_binary_operations.rb
Original file line number Diff line number Diff line change
@@ -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