Last active
August 16, 2016 21:41
-
-
Save trueskawka/7b7c5fdbcdfd99a6c5b4d125ec7f3ba5 to your computer and use it in GitHub Desktop.
bitwise task pair programming
This file contains 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
def bitwise(a, b) | |
array = [] | |
array_trunc = [] | |
for i in a..b do | |
array << i.to_s(2) | |
end | |
array.sort.reverse! | |
len = array[0].length | |
number = Array.new(len, 1) | |
array.each do |num| | |
diff = num.length - len | |
if diff > 0 | |
array_trunc << num[diff..-1] | |
else | |
array_trunc << num | |
end | |
end | |
array_trunc.each do |numt| | |
for i in 0..numt.length-1 do | |
if numt[i] == "0" | |
number[i] = 0 | |
end | |
end | |
end | |
number = number.join("") | |
return number.to_i(2) | |
end | |
p bitwise(12,15) | |
p bitwise(2,3) | |
p bitwise(8,13) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment