Integer Representations in Computer Organization and Assembly Language
In computer systems, numbers are represented using binary digits (bits). Since computers operate on binary logic (0s and 1s), different schemes are used to represent integers efficiently. The main integer representation methods are:
- Unsigned Representation
- Signed Magnitude Representation
- Two’s Complement Representation
- One’s Complement Representation
- Excess (or Bias) Representation
1. Unsigned Integer Representation
- Uses only positive numbers, with all bits representing the magnitude.
- A binary number with n bits can represent values from 0 to 2n−1.
- Example (8-bit representation):
- 000000002=0
- 000000012=1
- 111111112=255 (Max value for 8 bits)
🔹 Use case: Useful in scenarios where negative numbers are not needed (e.g., memory addresses).
2. Signed Magnitude Representation
- The most significant bit (MSB) is the sign bit (0 for positive, 1 for negative), and the remaining bits represent the magnitude.
- Range: −(2n−1−1) to +(2n−1−1)
- Example (8-bit representation):
- 000001012=+5
- 100001012=−5
🔹 Disadvantage:
- Has two representations for zero: 000000002=+0 and 100000002=−0, leading to inefficiency.
- Arithmetic operations (addition, subtraction) require additional logic.
3. Two’s Complement Representation (Most Common for Signed Integers)
-
The MSB is the sign bit (0 for positive, 1 for negative), but negative numbers are stored as the two’s complement of their magnitude.
-
To get the two’s complement of a number:
- Invert all bits (one’s complement).
- Add 1 to the result.
-
Advantages:
- Eliminates the problem of two representations for zero.
- Makes addition and subtraction simpler.
- Allows seamless wraparound in arithmetic operations.
-
Range for an n-bit system:
- −2n−1 to 2n−1−1
-
Example (8-bit representation of -5):
- 000001012=+5
- Invert bits: 111110102
- Add 1: 111110112=−5
-
Example values in 8-bit two’s complement:
- 011111112=127
- 100000002=−128 (smallest number)
- 111111112=−1
🔹 Why is it widely used?
- Efficient for arithmetic operations.
- Allows CPU to use the same hardware circuits for addition and subtraction.
4. One’s Complement Representation
-
Similar to two’s complement but without adding 1.
-
To represent a negative number, invert all bits.
-
Example (8-bit representation of -5):
- 000001012=+5
- Invert bits: 111110102 (One’s complement of -5)
-
Problems:
- Two representations of zero: 000000002=+0 and 111111112=−0.
- Arithmetic operations require handling of carry bits.
🔹 Not commonly used due to inefficiency in arithmetic operations.
5. Excess (Bias) Representation
-
Used primarily for floating-point numbers.
-
A fixed bias is subtracted from the stored value to get the actual value.
-
Example (Excess-127 in 8-bit representation):
- Bias = 127
- Stored value = Actual value + Bias
- If actual exponent = 5, stored = 5+127=132 (Binary: 100001002).
🔹 Used in IEEE 754 Floating-Point Representation.
Comparison of Representations
| Representation |
Range (for 8-bit) |
Zero Representations |
Arithmetic Efficiency |
| Unsigned |
0 to 255 |
1 |
Simple |
| Signed Magnitude |
-127 to 127 |
2 |
Complex |
| Two’s Complement |
-128 to 127 |
1 |
Efficient |
| One’s Complement |
-127 to 127 |
2 |
Less Efficient |
| Excess/Bias |
Floating Point |
1 |
Used in FP |
Conclusion
- Two’s complement is the most widely used method for representing integers in modern computers due to its arithmetic efficiency and simplicity.
- Unsigned representation is useful where negative numbers are not needed.
- One’s complement and signed magnitude are mostly historical and not used in modern CPUs.
- Excess representation is primarily used for floating-point arithmetic.