Most languages (Ruby included) allow number literals to be written in at least three bases: decimal, octal and hexadecimal. Numbers in decimal base is the usual thing and are written as (most) people naturally write numbers, 96 is written as 96
. Numbers prefixed by a zero are usually interpreted as octal based: 96 would be written as 0140
. Hexadecimal based numbers are usually prefixed by 0x
: 96 would be written as 0x60
.
The question is: can I write numbers as binary literals in Ruby? How?
use 0b prefix
>> 0b100
=> 4
and you can do:
>> easy_to_read_binary = 0b1110_0000_0000_0000
=> 57344
>> easy_to_read_binary.to_s(10)
=> "57344"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With