Data representation · foundation
Binary representation
Binary positional notation represents a non-negative integer using powers of two and the digits 0 and 1. A stored bit pattern acquires a more specific meaning only when interpreted with a width, type, and encoding convention.
Why it matters
Binary makes masks, shifts, capacity limits, powers of two, and low-level debugging predictable. It also prevents confusing raw patterns with signed numbers, text, floating-point values, or instructions.
Mental model
How to reason about binary representation
Starting at the right, each position has weight 1, 2, 4, 8, and so on. The represented unsigned value is the sum of the weights whose bits are 1; type rules can interpret the same finite pattern differently.
Analogy
Binary is a row of lamps with doubling values. A lit lamp contributes its labelled value, but a separate legend can assign a different interpretation to the entire pattern.
Examples
See the boundary, not just the happy path
Worked example · Convert binary to decimal
0b1101 = 8 + 4 + 1 = 13The set bits occupy positions weighted 2^3, 2^2, and 2^0. The zero in the 2^1 position contributes nothing.
Worked example · Pad to a declared width
13 as an 8-bit unsigned pattern is 00001101Leading zeroes do not change the unsigned value, but they make the eight-bit storage width explicit and group cleanly into two hexadecimal digits.
Useful contrast · Same bits, different interpretation
11111111 can mean unsigned 255 or signed -1 in 8-bit two's complementThe raw pattern does not carry its own signedness. The consuming type or format determines the mapping from pattern to value.
Common mistakes
Misconceptions to remove early
Reading binary digits as decimal digits
Binary 10 is two, not decimal ten. Each position grows by a factor of two instead of ten.
Assigning one universal meaning to a bit pattern
The same bits can encode an integer, floating-point number, text fragment, instruction, flags, or arbitrary bytes. Interpretation requires a schema or type.
Quick check
Can you predict the result?
1. What decimal value does unsigned binary 10110 represent?
- • 22
- • 18
- • 10110
2. Why can the pattern 11111111 represent either 255 or -1?
Keep building