Help File:Unsigned values
Jump to navigation
Jump to search
In computing, a value can be either signed or unsigned. Unsigned values can only represent zero and positive numbers, while signed values can represent both positive and negative numbers.
How Negative Numbers Are Stored (Two's Complement)[edit]
To store negative numbers, computers use a method called two's complement:
- Start with the binary representation of the positive number.
- Invert all the bits (change 0's to 1's and 1's to 0's).
- Add 1 to the result.
For example, to represent -12 in 8 bits:
- 12 in binary: 00001100
- Invert bits: 11110011
- Add 1: 11110100
So, 11110100
is -12 as a signed value.
If interpreted as an unsigned value, 11110100
would be 244 (since 256 - 12 = 244).
Why This Matters[edit]
- Unsigned values are useful when you know a value will never be negative (e.g., memory addresses, sizes, counters).
- Signed values are needed when negative numbers are possible (e.g., temperatures, scores, offsets).