stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 274

Day 274 of 365 · Expert · Month 10: Bits and data

Thinking in binary and hex

Bits and place values, hex as shorthand for four bits, binary and hex literals with underscores and the 53-bit limit of registers.

Lesson 15 min Week 40

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:

Bit76543210
Value1286432168421

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:

BinaryHexDecimal
%1010_0111$A7167
%1111_1111$FF255
%1_0000_0000$100256

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.

Warning

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?

Tip

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

The register bank of the chip r0 42 r1 31 r2 10 r3 -2.5 r4 0 r5 0 r6 0 r7 0 r8 0 r9 0 r10 0 r11 0 r12 0 r13 0 r14 0 r15 0 sp = r16 ra = r17 16 free registers, one number each sp: stack pointer · ra: return address
Registers store decimal numbers. Whole numbers are exact in them up to 2^53.

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

Esc
↑ ↓ selectEnter open/ opens search anywhere