Skip to content

Instantly share code, notes, and snippets.

@pallavsharma
Forked from alexvbush/decimal_to_binary.rb
Last active August 29, 2015 14:14
Show Gist options
  • Save pallavsharma/418d09f8524dfbb0bb11 to your computer and use it in GitHub Desktop.
Save pallavsharma/418d09f8524dfbb0bb11 to your computer and use it in GitHub Desktop.
def dec2bin(number)
number = Integer(number)
if(number == 0) then 0 end
ret_bin = ""
while(number != 0)
ret_bin = String(number % 2) + ret_bin
number = number / 2
end
ret_bin
end
# Integer("0b#{dec2bin 5}") will convert back to decimal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment