Skip to content

Instantly share code, notes, and snippets.

@bangedorrunt
Forked from alexvbush/decimal_to_binary.rb
Last active August 29, 2015 14:20
Show Gist options
  • Save bangedorrunt/94c5083bc127bb510991 to your computer and use it in GitHub Desktop.
Save bangedorrunt/94c5083bc127bb510991 to your computer and use it in GitHub Desktop.
Convert Decial to Binary
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