# Binary and Number Systems
## CS Foundations — The Codex Coding
**URL:** https://thecodex.expert/coding/foundations/binary-and-number-systems/
**Type:** concept
**Confidence:** high
**Last verified:** 2026-06-01
**Sources:** 5

---

## CANONICAL DEFINITION

A binary number system is a positional numeral system with a radix of 2, using only the digits 0 and 1, in which each digit position represents a successive power of 2 — the numeral system used by all digital computers because the two digits correspond directly to the two stable states (on/off) of the transistors from which computers are built.

---

## CONCEPT RELATIONSHIP MAP

**Requires first:** How Computers Work (foundations/how-computers-work)

**Enables understanding of:**
- Logic Gates (foundations/logic-gates)
- Data Types (concepts/data-types)
- Type Systems (how-languages-work/type-systems)
- Character Encoding / UTF-8 (foundations/encoding)

**Commonly confused with:**
- Floating-point rounding error (not a bug) versus integer overflow (a range violation)
- Hexadecimal as a different number vs. the same number in a different representation

---

## CURIOUS LEVEL

Computers count in binary using only 1s and 0s — not because binary is elegant, but because every switch inside a computer has exactly two positions: on and off.

### Positional Notation

Decimal (base 10) uses ten digits (0–9). Each column represents a power of 10. The number 347 = 3×100 + 4×10 + 7×1. Binary works identically but with base 2 and two digits.

### Binary

Binary 1011 = 1×8 + 0×4 + 1×2 + 1×1 = 11 in decimal.

One binary digit = 1 bit. Eight bits = 1 byte = 256 possible values (0–255). Common sizes:
- 8 bits (1 byte): 0 to 255 unsigned
- 16 bits (2 bytes): 0 to 65,535 unsigned
- 32 bits (4 bytes): 0 to 4,294,967,295 unsigned
- 64 bits (8 bytes): 0 to 18,446,744,073,709,551,615 unsigned

### Hexadecimal

Base 16. Digits: 0–9 and A–F (A=10, B=11, C=12, D=13, E=14, F=15). Four binary bits = one hex digit exactly. Prefix: 0x. Used for memory addresses, colour codes (HTML), file offsets, and hash values.

### Negative Numbers: Two's Complement

To represent -5 in 8-bit two's complement: take +5 (00000101), flip all bits (11111010), add 1 → 11111011. The leftmost bit signals negative. Range for 8-bit signed: -128 to +127. Adding two's complement numbers uses the same hardware as adding unsigned numbers.

### Floating Point

Real numbers use IEEE 754. A 32-bit float has: 1 sign bit, 8 exponent bits (bias 127), 23 mantissa bits. Key property: 0.1 cannot be represented exactly in binary. 0.1 + 0.2 ≠ 0.3 exactly. This is correct IEEE 754 behaviour, not a bug. Never use floats for money — use integers (store cents) or a Decimal type.

### In Code (Python)

```python
binary      = 0b11111111  # 255
hexadecimal = 0xFF        # 255
print(bin(255))   # '0b11111111'
print(hex(255))   # '0xff'
print(0.1 + 0.2)  # 0.30000000000000004
```

---

## EXPLORING LEVEL

### Positional Numeral Systems

Value = Σ (d_i × b^i) for i = 0 to n, where b is the radix and d_i are digits with 0 ≤ d_i < b.

### Binary Arithmetic

Addition rules: 0+0=0, 0+1=1, 1+0=1, 1+1=10 (0 carry 1).

Bitwise operations:
- AND (&): both bits must be 1
- OR (|): either bit is 1
- XOR (^): bits differ
- NOT (~): flip all bits
- Left shift (<<): multiply by 2^n
- Right shift (>>): integer divide by 2^n

### Hexadecimal

4 binary bits → 1 hex digit (0000=0, 1010=A, 1111=F). Appears in: memory addresses, RGB colours, SHA-256 hashes, UUID identifiers, MAC addresses.

### Two's Complement

n-bit range: -2^(n-1) to 2^(n-1) - 1.

8-bit: -128 to 127. 16-bit: -32,768 to 32,767. 32-bit: -2,147,483,648 to 2,147,483,647. 64-bit: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Algorithm to negate: flip all bits (one's complement), add 1.
Property: two's complement addition requires no special circuitry for signed vs. unsigned numbers.

### IEEE 754 Floating Point

Binary32 (float): 1 sign + 8 exponent (bias 127) + 23 mantissa = 32 bits.
Binary64 (double): 1 sign + 11 exponent (bias 1023) + 52 mantissa = 64 bits.
Value formula: (-1)^s × 2^(e-bias) × 1.f

Special values: ±0, ±∞ (exponent all 1, mantissa all 0), NaN (exponent all 1, mantissa nonzero), subnormals (exponent all 0, mantissa nonzero).

NaN ≠ NaN in all IEEE 754 languages. Use math.isnan() or equivalent to test.

### COMMONLY CONFUSED

- Floating-point imprecision is not a programming error. 0.30000000000000004 from 0.1+0.2 is the correct IEEE 754 answer.
- Two's complement is not the only negative representation — sign-magnitude exists in older protocols.
- Hexadecimal is not a different kind of number. 255 = 0xFF = 0b11111111 = the same value.
- Integer overflow ≠ floating-point error. Overflow exceeds type range; float error is within-range imprecision.

---

## DEEP DIVE LEVEL

### IEEE 754-2019 Specification

Binary64 structure: 1 sign bit (s), 11 exponent bits (e, biased 1023), 52 mantissa bits (f, implicit leading 1 for normalised values).

Value: (-1)^s × 2^(e-1023) × 1.f
Range: ~±5×10^-324 to ±1.8×10^308. Precision: ~15-17 significant decimal digits.

Special encodings:
- e=0, f=0: ±0
- e=0, f≠0: subnormal (denormalised)
- e=all-1, f=0: ±∞
- e=all-1, f≠0: NaN (quiet or signalling)

Subnormals allow gradual underflow near zero at the cost of precision.

### Two's Complement: Formal Properties

Two's complement arithmetic is modular arithmetic mod 2^n. The most negative n-bit value (-2^(n-1)) has no positive counterpart — it is its own negation (overflow in negation).

Overflow behaviour by language:
- C (ISO C17 §6.5): signed integer overflow is undefined behaviour
- Java (JLS §15.18.2): signed integer overflow wraps modularly
- Python: arbitrary precision integers, overflow impossible
- Rust (debug): panic on overflow; (release): wraps

### Unicode and UTF-8

Unicode assigns code points (U+0000 to U+10FFFF, 1,114,112 total). UTF-8 (RFC 3629) encodes as 1-4 bytes. ASCII characters (U+0000-U+007F) encoded as single bytes identical to ASCII values — UTF-8 is ASCII-compatible.

### Primary Sources

- IEEE Std 754-2019. IEEE Standard for Floating-Point Arithmetic. IEEE Computer Society.
- Patterson, D. A. & Hennessy, J. L. (2020). Computer Organization and Design. 6th ed. Morgan Kaufmann.
- Tanenbaum, A. S. & Austin, T. (2012). Structured Computer Organization. 6th ed. Pearson.
- The Unicode Consortium. The Unicode Standard. unicode.org.
- ISO/IEC 9899:2018 (ISO C17). International Organization for Standardization.

---

*The Codex Coding — thecodex.expert/coding/ — Free forever — Mumbai, India*
*Last verified: June 2026 · Source confidence: high · hello@thecodex.expert*
