What you learn today
- Pack values with ins
- Unpack with ext
- Save space in the stack
A logic memory holds exactly one number. If you want to pass temperature, pressure and oxygen of a room on to another chip, you need three memories – or you pack everything into one number. With ins and ext from bit week that is no witchcraft.
Planning fields
First the question: what range does each value have, and how many bits does it need?
| Value | Range | Bits | Position |
|---|---|---|---|
| Temperature in °C | 0–127 | 7 | bits 0–6 |
| Pressure in kPa | 0–255 | 8 | bits 7–14 |
| Oxygen in % | 0–100 | 7 | bits 15–21 |
n bits hold numbers from 0 to 2^n − 1. Together that is 22 bits – far below the limit of 53.
Packing with ins
Each value is rounded, limited to its range and then inserted. clamp makes sure an outlier does not spill into the neighbouring field:
sub value value CELSIUSOFFSET
round value value
clamp value value 0 127 # fits into 7 bits
ins packed value 0 7
Unpacking with ext
The receiver pulls the fields out again with the same numbers:
ext temp packed 0 7
ext press packed 7 8
ext o2 packed 15 7
Write the field table as a comment into both chips. If a field changes on one side only, the receiver reads nonsense without any error.
Shifting negative values
A field knows no negative numbers. On Mars it is well below 0 °C outside at night. So shift beforehand: °C + 50 turns −50 to 77 °C into the range 0 to 127. When unpacking you subtract the 50 again.
Saving space in the stack
The stack has 512 slots. A history with three values per reading lasts for 170 readings unpacked, for 512 packed – three times as long:
push packed # one slot for three values
If the sensor is missing, NaN becomes 0 when inserted. The packed result would then look like “0 °C, 0 kPa, 0 %” – a valid but wrong reading. So on a failure write a number that cannot be a real reading, for example −1.
Diagram
Exercise
Task
Pack the values of the gas sensor “Raum” into one number following the table above and write it into the logic memory “Paket”. Read the packet back as a check and show the unpacked oxygen on the display “Sauerstoff”. If the sensor is missing, write −1 into memory and display.
Hints
Hint 1
Each value: read, convert, round, clamp, ins into its place.
Hint 2
Oxygen: RatioOxygen times 100, then bits 15–21.
Hint 3
As a check: read the memory back with lbn and ext out value 15 7.
Quiz
4 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