Big idea
All data in a computer—numbers, text, images, instructions—ultimately reduces to binary patterns, sequences of 0s and 1s. To represent integers, a computer must map those bit-patterns to numerical values. The key distinction is:
- Unsigned integers use all available bits to represent only non-negative values.
- Signed integers dedicate one bit (usually the most significant bit) to encode positive and negative values.
Understanding these representations is essential for IB A1.2 (data representation), for debugging overflow behaviour in code, and for designing efficient systems.
1. Binary as a representation system
A computer stores integers as fixed-width binary values—commonly 8, 16, 32, or 64 bits.
For any n-bit integer, there are exactly 2ⁿ distinct bit-patterns. The meaning of those patterns depends on the representation scheme.
Example (8 bits, raw binary pattern):1100 1110
The pattern is just bits. How we interpret it determines the number it represents.
2. Unsigned integers
In an unsigned representation, all bits contribute to magnitude. There is no sign bit.
Range
For n bits:
- Minimum: 0
- Maximum: 2ⁿ − 1
Example (8-bit unsigned):
- Range: 0 to 255
- Pattern
1100 1110₂= 206₁₀
Key properties
- Can represent larger positive values with the same number of bits.
- Cannot represent negative values.
- Arithmetic wraps around on overflow modulo 2ⁿ.
This representation is used when negative numbers are impossible or meaningless (e.g., RGB pixel channels, array indices, memory addresses).
3. Signed integers
To represent negative values, computers introduce a sign convention. Historically, several methods were used (such as sign-magnitude and one’s complement), but modern systems universally use two’s complement.
Two’s complement is a signed integer representation in which the most significant bit—found at the far left of the binary number—indicates the sign. A value with an MSB of 0 is positive; a value with an MSB of 1 is negative. Negative values are formed by inverting all the bits of their positive magnitude and adding one. This scheme allows the same binary circuitry to perform addition and subtraction, provides a single representation for zero, and supports a continuous integer range from –2ⁿ⁻¹ to 2ⁿ⁻¹−1 in an n-bit system.
Two’s complement is a binary method for representing signed integers in which the most significant bit (the left-most bit) indicates the sign, and negative numbers are produced by inverting all bits of the positive value and adding 1. It provides a seamless arithmetic range and allows the same hardware circuits to perform both addition and subtraction.
Why two’s complement?
- It has a single representation for zero.
- The same circuitry handles addition and subtraction for both positive and negative values.
- Bit patterns “wrap through” zero smoothly.
Two’s complement structure
For n bits:
- MSB (most significant bit) is the sign bit.
- 0 = non-negative
- 1 = negative
Range
- Minimum: –2ⁿ⁻¹
- Maximum: +2ⁿ⁻¹ − 1
Example (8-bit signed):
- Range: –128 to +127
Interpreting two’s complement
Example bit pattern:1100 1110₂
Since MSB = 1, the number is negative.
Two common decoding approaches:
Method 1: Subtract 2ⁿ
Value = unsigned_value − 256
= 206 − 256
= –50
Method 2: Invert + add 1
- Invert bits:
0011 0001 - Add 1:
0011 0010= 50₁₀ - Apply sign → –50
Result:
1100 1110₂ = –50₁₀ in 8-bit signed form.
4. Comparing signed and unsigned
| Property | Unsigned | Signed (Two’s complement) |
|---|---|---|
| Range | 0 to 2ⁿ−1 | –2ⁿ⁻¹ to 2ⁿ⁻¹ − 1 |
| Negative numbers | Not allowed | Allowed |
| MSB meaning | Power of two | Sign indicator |
| Arithmetic | Mod 2ⁿ | Mod 2ⁿ but with signed interpretation |
| Zero representations | One | One |
| Hardware complexity | Simpler | Slightly more complex but unified for add/sub |
5. Why computers prefer two’s complement
- Natural wrapping: adding 1 to 1111 1111₂ produces 0000 0000₂.
- Efficient ALU design: subtraction becomes addition of the negated operand.
- Logical ordering: bit patterns sort correctly when interpreted as signed numbers.
These properties make two’s complement essential in CPU design, instruction sets, and compiler behaviour—important links back to A1.2 and A1.1 of the IB syllabus.
6. Example: 16-bit comparison
Consider the decimal value 40000.
- Unsigned 16-bit max = 65535 → representable.
- Signed 16-bit max = 32767 → not representable; overflow occurs.
Same bit pattern, different meaning:
1001 1100 0100 0000₂ =
40000 (unsigned)
–25536 (signed, two’s complement)
This example is useful for IB Paper 1 questions about overflow and data interpretation.
7. Consequences in programming
Example 1: C, C++, Java, Python (behind the scenes)
- Casting between signed/unsigned changes interpretation, not bits.
- Array lengths and memory addresses use unsigned semantics.
- Loop counters and arithmetic often use signed integers by default.
Example 2: Image representation
RGB values 0–255 require unsigned 8-bit fixed-width integers.
Negative values would be meaningless.
Example 3: Sensor data or offsets
Temperature deltas or velocity changes may be negative → signed needed.
8. Command term alignment for IB
If a student is asked to describe two’s complement, a high-quality answer should:
- Identify the MSB as the sign bit.
- Explain the range asymmetry (e.g., –128 to 127 in 8 bits).
- Explain the rule for forming negative values (invert + add 1).
- Refer explicitly to binary representation.
A less good “describe” response would only state that “two’s complement stores negative numbers” without explaining how or why.
9. Worked numerical example for students
Task: Represent –23 in 8-bit two’s complement.
- Represent magnitude:
23₁₀ =0001 0111₂ - Invert bits:
1110 1000 - Add 1:
1110 1001
Final result: 1110 1001₂
Check:
Invert: 0001 0110
Add 1: 0001 0111 = 23 → negative, so –23
10. Summary
- Binary is a universal representation system; numbers are just patterns.
- Unsigned integers use all bits for magnitude and represent only non-negative values.
- Signed integers dedicate the MSB for sign and use two’s complement to encode negative values.
- Two’s complement simplifies hardware, supports efficient arithmetic, and ensures unique zero.
- Interpretation (signed vs unsigned) completely changes the meaning of a bit-pattern.
This concept underpins CPU operation, data integrity, memory addressing, and virtually every algorithm a student will write.