From Bits to Meaning

type
lab
status
draft
id
lab.from-bits-to-meaning

Lab: Inspect Meaning In Bytes

Goal

Use local commands to see that files contain bytes, and that the interpretation of those bytes depends on a code or convention.

Setup

These commands are macOS-friendly and use common local tools:

Work in /tmp so the lab does not modify the repository.

mkdir -p /tmp/from-bits-to-meaning-lab
cd /tmp/from-bits-to-meaning-lab

Step 1: Make A Tiny Text File

printf 'Hi, 12!\n' > message.txt
xxd -g 1 message.txt
file message.txt

Observe:

  • xxd shows bytes in hexadecimal.
  • In ASCII-compatible text, H is one byte, i is one byte, punctuation is one byte, and the digit characters 1 and 2 are bytes too.
  • The file does not store the abstract number twelve. It stores character codes for the text symbols 1 and 2.

Step 2: Ask Python To Show Several Interpretations

python3 - <<'PY'
data = b"Hi, 12!\n"
for index, byte in enumerate(data):
    char = chr(byte) if 32 <= byte <= 126 else repr(chr(byte))
    print(index, hex(byte), format(byte, "08b"), byte, char)
PY

Observe:

  • The same byte can be shown as hex, binary, decimal, or a character.
  • These views are interpretations of the same stored pattern.

Step 3: Compare Text 12 With Binary Number 12

python3 - <<'PY'
from pathlib import Path

Path("number-as-text.txt").write_text("12\n", encoding="ascii")
Path("number-as-one-byte.bin").write_bytes(bytes([12]))
Path("number-as-two-byte-little.bin").write_bytes((12).to_bytes(2, "little"))
PY

xxd -g 1 number-as-text.txt
xxd -g 1 number-as-one-byte.bin
xxd -g 1 number-as-two-byte-little.bin
file number-as-text.txt number-as-one-byte.bin number-as-two-byte-little.bin

Observe:

  • number-as-text.txt stores character codes for the symbols 1, 2, and newline.
  • number-as-one-byte.bin stores the numeric value 12 in one byte.
  • number-as-two-byte-little.bin stores the numeric value 12 in two bytes using little-endian byte order.

This connects to Petzold’s distinction between ASCII text files and binary files.

Step 4: Same Byte, Different Stories

python3 - <<'PY'
byte = 0x41
print("hex:", hex(byte))
print("binary:", format(byte, "08b"))
print("unsigned integer:", byte)
print("ASCII-compatible character:", chr(byte))
print("as two 4-bit nibbles:", format(byte >> 4, "04b"), format(byte & 0x0F, "04b"))
PY

Observe:

  • 0x41 can be read as 65 or as A.
  • Nothing inside the byte forces one interpretation.
  • The interpreter supplies the meaning.

Step 5: Pack Eight Black/White Pixels Into One Byte

python3 - <<'PY'
pixels = [0, 1, 0, 1, 1, 0, 0, 1]
value = 0
for bit in pixels:
    value = (value << 1) | bit

print("pixels:", pixels)
print("packed byte:", hex(value), format(value, "08b"), value)
print("visual:", "".join("#" if bit else "." for bit in pixels))
PY

Observe:

  • The same eight bits can be treated as a row of black/white pixels.
  • A display or image format contract decides which bit corresponds to which pixel and which value means black or white.

Reflection

Answer these in your own words:

Source Anchors

Open Questions