What you learn today
- Bits and place values
- Hex as shorthand
- 53 bits of precision
Twelve lights, twelve yes-or-no values. So far you have used a register for each, and with sixteen registers that runs out quickly. But a single number holds far more yes-or-no values if you read it as bits. That is why the expert level starts by looking inside the number.
Place values
A binary number only has the digits 0 and 1. Each place is worth twice as much as the one to its right:
| Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
So %1010_0111 is 128 + 32 + 4 + 2 + 1 = 167. Bit 0 is always on the right. The chip ignores the underscores, they make long numbers readable.
Hex as shorthand
Four bits give 16 possibilities, exactly one hex digit from 0 to F. That is why hex is the shorthand for binary numbers:
| Binary | Hex | Decimal |
|---|---|---|
%1010_0111 | $A7 | 167 |
%1111_1111 | $FF | 255 |
%1_0000_0000 | $100 | 256 |
You read $A7 as “A = 1010, 7 = 0111”. Each hex digit stands for exactly four bits.
move r0 %1010_0111 # 167
move r1 $A7 # the same number
move r2 $FF_FF # 65535, underscore here too
Places without bit instructions
Before the bit instructions arrive tomorrow: with div, floor and mod you can reach single places too. Dividing by 16 moves one hex digit to the right, mod 16 keeps the last one:
div high number 16
floor high high # upper hex digit
mod low number 16 # lower hex digit
You get bit 3 the same way: divide by 8, round down, mod 2.
53 bits of precision
Registers store decimal numbers. Whole numbers are exact in them, but only up to 2^53 = 9,007,199,254,740,992. Above that the last digits get lost: 2^53 + 1 cannot be stored and becomes 2^53.
For hashes that is plenty, they are 32-bit numbers. But anyone packing many values into one number runs into the 53 bits. So always plan packed values with bit counts: how many bits does each field need, and does the total fit into 53?
The preprocessor converts % and $ into decimal numbers when loading. For the character limit, however, the written form counts: %1111_1111 costs ten characters, 255 only three.
Diagram
Exercise
Task
number (r0) holds a number from 0 to 255. Without bit instructions:
high= upper hex digit (0–15)low= lower hex digit (0–15)bit3= bit 3 of the number (0 or 1)plus= number + 256, with 256 written as the hex literal$100
Hints
Hint 1
Upper hex digit: div by 16, then floor.
Hint 2
Lower hex digit: mod … 16.
Hint 3
Bit 3: divide by 8, round down, mod 2. For plus: add plus number $100.
Quiz
5 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in