COA/Lesson 1 of 6/7 min read
Number Systems
Binary, octal and hexadecimal conversion, signed numbers with two's complement, overflow, IEEE 754 floating point, BCD and character encodings.
On this page
Number System
A positional number system with base b uses digits 0 to b - 1; each position is worth a power of the base.
(d_n ... d_1 d_0)_b = d_n * b^n + ... + d_1 * b^1 + d_0 * b^0| System | Base | Digits | Use |
|---|---|---|---|
| Binary | 2 | 0, 1 | Hardware |
| Octal | 8 | 0 to 7 | Unix permissions (chmod 755) |
| Decimal | 10 | 0 to 9 | Humans |
| Hexadecimal | 16 | 0 to 9, A to F | Memory addresses, colors, bytes |
Binary Number
Computers use binary because transistors have two reliable states. Units:
| Unit | Size |
|---|---|
| Bit | 1 binary digit |
| Nibble | 4 bits |
| Byte | 8 bits |
| Word | Natural size for the CPU (32 or 64 bits) |
| KiB / KB | 1024 bytes (binary) / 1000 bytes (SI) |
With n bits you can represent 2^n values: unsigned range 0 to 2^n - 1. A byte holds 0 to 255.
Hexadecimal
Each hex digit is exactly 4 bits, so conversion between binary and hex is grouping:
1101 0111 1010 (binary)
D 7 A (hex) = 0xD7AOctal groups 3 bits at a time: 110 101 111 = 657 octal.
Base Conversion
Decimal to binary
Repeatedly divide by 2 and read the remainders bottom up.
45 / 2 = 22 r 1
22 / 2 = 11 r 0
11 / 2 = 5 r 1
5 / 2 = 2 r 1
2 / 2 = 1 r 0
1 / 2 = 0 r 1
45 = 101101 (binary)Binary to decimal
Sum the powers of two: 101101 = 32 + 8 + 4 + 1 = 45.
Fractions
Multiply the fractional part by 2 repeatedly and take the integer parts top down: 0.625 = 0.101 binary. Some decimals like 0.1 have no finite binary representation, which is why 0.1 + 0.2 != 0.3 in floating point.
Signed numbers
Sign-Magnitude
The leftmost bit is the sign, the rest is the magnitude. Simple, but has two zeros (+0 and -0) and addition needs special logic.
One's Complement
Negate by flipping all bits. Still has two zeros; addition needs an end-around carry.
Two's Complement
Negate by flipping all bits and adding 1. Used by virtually all modern CPUs.
5 = 0000 0101
-5 = 1111 1010 + 1 = 1111 1011Why it wins:
- Only one zero.
- Addition and subtraction use the same circuit as unsigned arithmetic; subtraction is adding the negation.
- The most significant bit still indicates the sign.
Range with n bits: -2^(n-1) to 2^(n-1) - 1. An 8-bit signed byte is -128 to 127. Note the asymmetry: negating -128 overflows back to -128.
| Representation | 8-bit range | Zeros |
|---|---|---|
| Unsigned | 0 to 255 | 1 |
| Sign-magnitude | -127 to 127 | 2 |
| One's complement | -127 to 127 | 2 |
| Two's complement | -128 to 127 | 1 |
Sign extension: when widening a signed number, copy the sign bit into the new high bits. 1011 (4-bit -5) becomes 1111 1011 (8-bit -5).
Overflow
The result does not fit in the available bits.
- Unsigned: a carry out of the most significant bit.
255 + 1wraps to0in 8 bits. - Signed (two's complement): adding two positives gives a negative, or two negatives give a positive. Equivalent rule: the carry into the sign bit differs from the carry out of it.
Real bug: the binary search midpoint (lo + hi) / 2 overflows in 32-bit ints for large arrays; use lo + (hi - lo) / 2.
Floating Point
Represents real numbers in scientific notation: value = (-1)^sign * 1.mantissa * 2^(exponent - bias).
IEEE 754
| Format | Total bits | Sign | Exponent | Mantissa (fraction) | Bias | Decimal digits |
|---|---|---|---|---|---|---|
| Single (float) | 32 | 1 | 8 | 23 | 127 | About 7 |
| Double | 64 | 1 | 11 | 52 | 1023 | About 15 to 16 |
Example: -6.5 in single precision.
6.5 = 110.1 (binary) = 1.101 * 2^2
sign = 1
exponent = 2 + 127 = 129 = 1000 0001
mantissa = 101 0000 ... (leading 1 is implicit)
bits = 1 10000001 10100000000000000000000Special values
| Exponent bits | Mantissa | Meaning |
|---|---|---|
| All 0 | 0 | Zero (positive or negative) |
| All 0 | Non-zero | Denormal (subnormal) number |
| All 1 | 0 | Infinity |
| All 1 | Non-zero | NaN (not a number) |
Pitfalls:
- Never compare floats with
==; checkabs(a - b)against a small epsilon. NaN != NaNis true.- Never store money in floats; use integers (cents) or decimal types.
- Adding a tiny number to a huge one can do nothing (loss of precision).
BCD
Binary Coded Decimal stores each decimal digit in 4 bits: 59 = 0101 1001. Wastes space (6 of 16 patterns unused) but makes decimal display and exact decimal arithmetic easy. Used in clocks, calculators and some financial hardware.
Gray Code
Consecutive values differ in exactly one bit: 000, 001, 011, 010, 110, 111, 101, 100. Prevents glitches in rotary encoders and is used in Karnaugh map ordering. Convert: gray = n XOR (n >> 1).
Endianness
Order of bytes for multi-byte values in memory. Storing 0x12345678 at address 100:
| Address | Big-endian | Little-endian |
|---|---|---|
| 100 | 12 | 78 |
| 101 | 34 | 56 |
| 102 | 56 | 34 |
| 103 | 78 | 12 |
x86 and most ARM systems are little-endian. Network protocols use big-endian ("network byte order"), so code calls htonl and ntohl when sending integers.
Character encodings
| Encoding | Size | Notes |
|---|---|---|
| ASCII | 7 bits | 128 characters; 'A' = 65, 'a' = 97, '0' = 48 |
| Unicode | Code points | Over a million possible characters |
| UTF-8 | 1 to 4 bytes | ASCII compatible, dominant on the web |
| UTF-16 | 2 or 4 bytes | Used by Java and JavaScript strings internally |
Quick revision
- Hex digit = 4 bits, octal digit = 3 bits; convert by grouping.
- Decimal to binary: divide by 2, read remainders bottom up.
- Two's complement: invert and add 1; range
-2^(n-1)to2^(n-1) - 1; one zero. - Signed overflow: two same-sign operands give an opposite-sign result.
- IEEE 754 single: 1 sign, 8 exponent (bias 127), 23 mantissa; double: 1, 11 (bias 1023), 52.
0.1is not exact in binary; never compare floats with==or store money in them.- Little-endian stores the least significant byte first; network order is big-endian.