Viewing a single comment thread. View all comments

jaa101 t1_j6h2q5u wrote

Digits are usually represented by their ASCII code, with '0' having code 48 (binary 00110000) and '9' having code 57 (binary 00111001). So for a single digit the first step is to simply subtract 48. That would convert your '4' to binary 00000100.

For multi-digit whole numbers, the computer works from the first (leftmost) digit. For each ASCII digit it subtracts 48. If there's another digit following it multiplies its current answer by ten then continues, adding future digits to the total.

So, for '1234' (ASCII 49, 50, 51, 52) it goes:

  • 49 − 48 = 1
  • ×10 = 10 (binary 1010)
  • add 50 − 48 = 12 (binary 1100)
  • ×10 = 120 (binary 1111000)
  • add 51 − 48 = 123 (binary 1111011)
  • ×10 = (binary 100110011110)
  • add 52 − 48 = 1234 (binary 10011010010)
  • No more digits so we stop there.
4

GalFisk t1_j6hasdi wrote

And computers do this lightning fast. They can perform several billion instructions per second.

1