---
title: "8 bits of the value 81"
config:
packet:
bitsPerRow: 8
---
packet-beta
0: "0"
1: "1"
2: "0"
3: "1"
4: "0"
5: "0"
6: "0"
7: "1"
A data structure is a data organization and storage format.
Usually chosen for efficient access to data.
… a story of packing things 📦
Core & SD memory by Daniel Sancho from Málaga, Spain, CC BY 2.0
---
title: "8 bits of the value 81"
config:
packet:
bitsPerRow: 8
---
packet-beta
0: "0"
1: "1"
2: "0"
3: "1"
4: "0"
5: "0"
6: "0"
7: "1"
interpretation:
---
title: "bit values of an 8 bit unsigned integer"
config:
packet:
bitsPerRow: 8
---
packet-beta
0: "128"
1: "64"
2: "32"
3: "16"
4: "8"
5: "4"
6: "2"
7: "1"
\[ 0 \cdot 128 + 1 \cdot 64 + 0 \cdot 32 + 1 \cdot 16 + 0 \cdot 8 + 0 \cdot 4 + 0 \cdot 2 + 1 \cdot 1 = 81 \]
---
title: "8 bits of the value 81"
config:
packet:
bitsPerRow: 8
---
packet-beta
0: "0"
1: "1"
2: "0"
3: "1"
4: "0"
5: "0"
6: "0"
7: "1"
used in upcoming slides:
---
title: "showing value"
config:
packet:
bitsPerRow: 8
---
packet-beta
0-7: "81"
---
title: "using hexadecimal"
config:
packet:
bitsPerRow: 8
---
packet-beta
0-3: "0x5"
4-7: "0x1"
---
title: "bit values of an 8 bit signed integer"
config:
packet:
bitsPerRow: 8
---
packet-beta
0: "-128"
1: "64"
2: "32"
3: "16"
4: "8"
5: "4"
6: "2"
7: "1"
note, it’s -128!
this representation is called two’s complement
another (very uncommon, but obvious) option:
---
title: "sign and value representation"
config:
packet:
bitsPerRow: 8
---
packet-beta
0: "sign"
1-7: "value"
---
title: "32 bit float (single precision)"
config:
packet:
bitsPerRow: 32
---
packet-beta
0: "sign"
1-8: "exponent"
9-31: "fraction"
\[ \textrm{value} = (-1)^\textrm{sign} \cdot 2^{\textrm{exponent} - 127} \cdot \left( 1 + \textrm{fraction} \right) \]
exponent is an unsigned integerfraction is a binary fraction:---
title: "bit values of the fraction"
config:
packet:
showBits: false
bitsPerRow: 23
---
packet-beta
0: "1/2"
1: "1/4"
2: "1/8"
3: "1/16"
4: "1/32"
5: "1/64"
6-22: "..."
a character set (e.g. ASCII) assigns a different meaning to each stored number:
| decimal | hex | meaning |
|---|---|---|
| 10 | 0x0a |
new line |
| 48 | 0x30 |
0 |
| 63 | 0x3f |
? |
| 65 | 0x41 |
A |
| 97 | 0x61 |
a |
(these day’s we use UTF-8 instead of ASCII, which uses more than one byte, such that characters of all languages can be represented)
representation: the bit-pattern (0b01010001 or 0x51)
value: the interpretation (81 or Q)
The same bit-pattern may be interpreted differently depending on the type!
Take the bit-representation of a 32 bit float value of 1.0 and re-interpret it as an integer. Which value do you get? What’s the bit-representation?
numpy as this provides more fine-grained control about the exact primitive types.<f4, a 32 bit unsigned integer the dtype <u4..view(dtype) can be used to re-interpret memory as different type.1065353216 00111111100000000000000000000000
Memory is usually addressed (numbered) byte-wise.
From now, we’ll use byte-addressing instead of bit-addressing in the figures.
Multiple bits form primitive types.
Multiple primitive types form compound types
struct or @dataclass, tuple etc… form a fixed group of elements, each with a distinct meaning
---
title: "2x4 byte (32 bit) complex number"
config:
packet:
bitsPerRow: 8
---
packet-beta
0-3: "real part"
4-7: "imaginary part"
\[ \textrm{value} = \textrm{real part} + i \cdot \textrm{imaginary part} \]
The groupings of bits and bytes we’ve seen so far use fixed, pre-defined and agreed-on sequences of co-located memory locations to provide meaning to those memory locations.
To make use of the bits, we have to know what to expect.
Are there other (less fixed) ways to group and provide meaning to memory locations?
a series of consecutive memory cells
element access is easy
appending: easy if free space available
insert or removal is expensive
Arrays are containers, which store data elements in consecutive memory cells.
other names for similar things: vector, string
What metadata might be relevant for arrays?
e.g.:
TLV is a typical data structure for data acquisition 1 2
---
title: "TLV temperature data"
config:
packet:
bitsPerRow: 24
---
packet-beta
0-3: "TEMP"
4-7: "4 (or 16)"
8-11: "12.4"
12-15: "17.2"
16-19: "21.1"
20-23: "23.6"
TEMP for temperature as float)TLV is a combination of a struct and an array.
An integer (stored in memory) can be interpreted as a byte-address in memory (where another variable is located).
This is called a pointer.

The term “reference” is used for variable names which act like pointers, but hide their pointer-like nature.
Python usually doesn’t expose pointers directly, but references are everywhere.
Pointers (and thus references) can be used to form more complex relations and data structures.
Variable size, can but needn’t be contiguous in memory 
Next element is at the address saved in the additional pointer
The previous element can only be found by starting at the beginning
Element removal only requires to change a pointer address 
Element insertion only requires allocating memory for the new value and setting two pointers

list is more like an array of pointersalso called associative array, key-value store, map(ping), object etc…
Access elements by key (e.g. name) instead of position
Key space is often (infinitely) large (e.g. all possible strings)
There can’t be one “slot” for every possible key
A dictionary as a list works and is useful for data exchange, but getting an element requires accessing all of them (slow).
Better options for efficient access:
Sets are dictionaries without values.
Depending on the computer architecture or application, the ordering of the bits may be reversed:
---
title: "MSB first 8 bit integer"
config:
packet:
bitsPerRow: 8
---
packet-beta
0: "128"
1: "64"
2: "32"
3: "16"
4: "8"
5: "4"
6: "2"
7: "1"
---
title: "LSB first 8 bit integer"
config:
packet:
bitsPerRow: 8
---
packet-beta
0: "1"
1: "2"
2: "4"
3: "8"
4: "16"
5: "32"
6: "64"
7: "128"
BCD numbers use four bits to encode a digit (0 … 9).
The possible values 10 … 15 are unused.
---
title: "4 bit integer"
config:
packet:
bitsPerRow: 4
---
packet-beta
0: "8"
1: "4"
2: "2"
3: "1"
These digits can be combined to form a decimal number.
---
title: "4 digit BCD number"
config:
packet:
bitsPerRow: 16
---
packet-beta
0-3: "1000"
4-7: "100"
8-11: "10"
12-15: "1"
(it’s mostly an ancient and wasteful thing, but reduces complexity for transformations between bit representation and decimal value interpretation)
Only add or remove elements at the end (LIFO: last in, first out)
(i.e. like an array with enough space at the end)
Add at the end, remove at the front (FIFO: first in, first out)
['First', 'Second']
can be implemented using an array as a circular buffer
hash map