How do you write a binary literal in ruby?

后端 未结 4 687
忘掉有多难
忘掉有多难 2021-02-18 13:33

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 wr

相关标签:
4条回答
  • 2021-02-18 14:07

    For literals, the prefix is 0b. So

    0b100 #=> 4
    

    Be aware that the same exists to format strings:

    "%b" % 4 #=> "100"
    
    0 讨论(0)
  • 2021-02-18 14:24

    and you can do:

    >> easy_to_read_binary = 0b1110_0000_0000_0000
    => 57344
    >> easy_to_read_binary.to_s(10)
    => "57344"
    
    0 讨论(0)
  • 2021-02-18 14:30

    From this manual

    0b01011
    

    binary integer

    0 讨论(0)
  • 2021-02-18 14:31

    use 0b prefix

    >> 0b100
    => 4
    
    0 讨论(0)
提交回复
热议问题