-
-
Notifications
You must be signed in to change notification settings - Fork 302
Add bit_manipulation algorithms #139
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
Vijay-Siva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
|
||
Vijay-Siva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
Vijay-Siva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
begin | ||
puts is_bit_set(0, -4) | ||
rescue => e | ||
puts e.message | ||
end | ||
# position must be > 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.