Binary, hex and bits
We count with ten digits: after 9 they run out, so we write a 1 in front and start again at 0. A computer has only two digits, 0 and 1 – it counts in binary, and has to carry much sooner. to_s(2) writes a number in binary, and rjust from lesson 19 pads it with zeros in front:
(0..8).each do |n|
puts "#{n}: #{n.to_s(2).rjust(4, "0")}"
endEvery place is worth twice the one to its right: 1, 2, 4, 8, 16 … So 1101 is 8 + 4 + 0 + 1 = 13. to_i(2) reads a string of binary digits. In code you can write a number in binary straight away with 0b, in hexadecimal with 0x and in octal (base 8) with 0o – it is still the same number, only spelt differently:
p "1101".to_i(2)
p 0b1101
p 0b1101 == 13
p 0xff
0o17Hexadecimal counts with sixteen digits: 0 to 9, then a to f for 10 to 15. Two hex digits make exactly one byte, 0 to 255 – that is why bytes are so often written in hex, like the code points in lesson 20. A web colour like "#ff8800" is three bytes: red ff, green 88, blue 00. to_i(16) reads hex, to_s(16) writes it, and %02x in format always writes two digits:
p "ff".to_i(16)
p "88".to_i(16)
p 136.to_s(16)
p format("%02x", 8)
format("#%02x%02x%02x", 255, 136, 0)Bits can be combined place by place. & (AND) gives a 1 where both numbers have a 1, | (OR) where at least one has, ^ (XOR) where exactly one has. >> shifts all the bits to the right, which halves the number at every step, and << shifts them to the left, which doubles it. %04b in format shows four binary digits:
a = 0b1100
b = 0b1010
puts format("%04b", a & b) # AND
puts format("%04b", a | b) # OR
puts format("%04b", a ^ b) # XOR
puts format("%04b", a >> 2) # shifted right by 2
1 << 10 # 1, doubled ten timesA single bit you get with square brackets: n[0] is the lowest bit, the one worth 1 – so it tells you whether a number is odd. digits(2) gives all the bits, the lowest first, and bit_length says how many places the number needs:
n = 13 # 1101
p n[0] # worth 1
p n[1] # worth 2
p n.digits(2)
p n.bit_length
n.to_s(2).count("1")What are bits good for? For flags: several yes-or-no answers packed into one number. On Linux and macOS every file has three permissions – read (4), write (2) and run (1). Each is a bit of its own, so they add up without getting in each other's way: 6 is read and write. | switches a flag on, & asks for it – 0 means off – and allbits? asks more readably. (a, b = 1, 2 fills several variables at once.)
read, write, run = 4, 2, 1
recipe = read | write # read and write: 6
p recipe & write # not 0: write is on
p recipe & run # 0: run is off
p recipe.allbits?(write)
recipe |= run # switch run on, too
recipe.to_s(2)And XOR has a property that makes it the darling of secret codes: XOR with the same key twice, and you are back where you started – like ROT13 in lesson 24, where going round twice brought every letter home. Here on the bytes of a word:
key = 42
locked = "bacon".bytes.map { |b| b ^ key }
p locked
p locked.map(&:chr).join
locked.map { |b| (b ^ key).chr }.joinReal ciphers use far longer keys, but XOR is at the heart of many of them. Remember: to_s(base) writes a number in another base, to_i(base) reads it back; 0b, 0o and 0x write it straight into the code; &, |, ^ and the shifts work bit by bit.
hex_to_rgb(colour), which turns a web colour into its three bytes: hex_to_rgb("#ff8800") gives [255, 136, 0]. Capital letters, as in "#FFCC00", must work too.# def hex_to_rgb(colour)
# ... # "#ff8800" -> [255, 136, 0]
# end
# hex_to_rgb("#ff8800")
ls -l shows the permissions of a file as nine letters: rwx for its owner, then for its group, then for everybody else, with a - for every permission that is off (x stands for execute, run). The nine bits behind them are written in octal, because every octal digit is exactly three bits, one group: 0o754 is rwxr-xr--. Write permissions(mode), which turns such a number into its nine letters.# def permissions(mode)
# ... # 0o754 -> "rwxr-xr--"
# end
# permissions(0o754)