# Information Encoding and Binary Representation

Computer Science I · Introduction to Computers · https://tryals.app/learn/computer-science-i/information-encoding-and-binary-representation

## Everything Is Bits

A computer stores all information using just two states: on and off, represented as **0** and **1**. A single digit is a **bit**, and eight bits make a **byte**. 

What a bit pattern means depends entirely on its **interpretation**. The exact same byte $01000001$ can be the decimal number $65$, the letter $A$ in ASCII, or part of an image. 

| Binary | Powers | Decimal |
| --- | --- | --- |
| $1011$ | $8+2+1$ | $11$ |
| $10000$ | $16$ | $16$ |
| $11111111$ | $128+64+\dots+1$ | $255$ |

**Binary** is base-2, where positions are powers of $2$. An $n$-bit value gives $2^n$ combinations; an 8-bit byte holds $2^8 = 256$ values ($0$ to $255$).

> **Common pitfall:** Assuming a bit pattern has an inherent meaning. Always check how data is interpreted.

## Hex, Text, and Formulas

Because long binary strings are clumsy, programmers use **hexadecimal** (base 16, digits $0$–$9$ and $A$–$F$). Each hex digit packs 4 bits, so one byte equals two hex digits.

Text uses numeric mapping: **ASCII** uses 7 bits for 128 English characters, while **Unicode** extends this globally to cover every writing system.

The decimal value $d$ of any binary sequence is calculated by summing each bit $b_i$ multiplied by its position's power of 2:

$$d = \sum_{i=0}^{n-1} b_i \cdot 2^i$$

**Symbol Guide:**
- $d$: The resulting decimal number
- $n$: Total number of bits
- $b_i$: The bit value ($0$ or $1$) at position $i$
- $2^i$: The power of 2 for position $i$

## Practice questions

3 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. The byte 01000001 is the number 65 or the letter A depending on how the program interprets it, not on anything stored with the byte.

**Answer:** True

**Why:** True. The byte carries no tag; the meaning comes from the instruction that reads it. That is why a type error can reinterpret text as an integer without any hardware objection, why `char` and `int8` differ only in the compiler's bookkeeping, and why file formats need magic numbers to announce what they are.

Page: https://tryals.app/practice/computer-science-i/information-encoding-and-binary-representation/the-byte-01000001-is-the-number-65-or-the-letter-a-depending-on-how

### 2. A text file opened as an image produces coloured noise rather than an error message. Why does the computer not object?

A. Because the viewer detected the mismatch and substituted noise as a warning
B. Because the file was silently converted to an image format on opening
C. Because text and image formats share a common encoding
D. Because the bytes carry no type, so the viewer reads them as pixel values

**Answer:** D. Because the bytes carry no type, so the viewer reads them as pixel values

**Why:** There is no type tag in the bytes, the viewer simply reads them as pixels and paints the result. File extensions and magic numbers are conventions layered on top by software, which is why renaming a file changes nothing about it and why parsing untrusted input is where so many security holes live.

Page: https://tryals.app/practice/computer-science-i/information-encoding-and-binary-representation/a-text-file-opened-as-an-image-produces-coloured-noise-rather-than-an

### 3. Four bits can hold the values 0 to 15. Sort each number by whether four bits are enough to store it.

**Answer:**

- Fits in 4 bits: 15, 9, 0
- Needs more than 4 bits: 16, 31, 20

**Why:** Four bits give $2^{4} = 16$ patterns, numbered 0 to 15, so 15 fits exactly and 16 does not. Confusing the number of patterns (16) with the largest storable value (15) is the standard off-by-one of binary sizing, and it is the same mistake as expecting a byte to reach 256.

Page: https://tryals.app/practice/computer-science-i/information-encoding-and-binary-representation/four-bits-can-hold-the-values-0-to-15-sort-each-number-by-whether
