Skip to content

Instantly share code, notes, and snippets.

@k2052
Created January 20, 2012 22:55
Show Gist options
  • Save k2052/1650069 to your computer and use it in GitHub Desktop.
Save k2052/1650069 to your computer and use it in GitHub Desktop.
Add two binary strings using ruby.
class Numeric
def ones_complement(bits)
self ^ ((1 << bits) - 1)
end
end
def addbinary(b1, b2)
result = ''
carry = 0
b1 = b1.reverse
b2 = b2.reverse
b1.each_char.with_index do |char, i|
k1 = char
k2 = b2[i]
intCheck = k1.to_i + k2.to_i
if intCheck == 2
write = '10'
else
write = intCheck
end
result.insert(0, write.to_s)
end
return result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment