Loading lessons...
Signed Numbers, Complements, Fixed Point & IEEE 754 Floating Point
Signed Numbers, Complements, Fixed Point & IEEE 754 Floating Point
Hardware has no minus sign and no decimal point; it has only bits. This lesson shows how a computer stores negative numbers (sign-magnitude, 1's complement, 2's complement), why it subtracts by adding complements, how it detects overflow, and how it stores fractions in fixed-point and floating-point (IEEE 754) formats.
1. Unsigned vs Signed Numbers
An unsigned n-bit number uses every bit for magnitude: range 0 to 2n - 1. A signed number gives up the leftmost bit as a sign bit: 0 for positive, 1 for negative. There are three ways to code the negative values.
| Representation | How -N is formed | +45 in 8 bits | −45 in 8 bits |
|---|---|---|---|
| Sign-magnitude | sign bit 1, magnitude unchanged | 00101101 | 10101101 |
| 1's complement | invert every bit of +N | 00101101 | 11010010 |
| 2's complement | invert every bit, then add 1 | 00101101 | 11010011 |
Positive numbers look the same in all three. Only negative numbers differ.
Sign-magnitude, 1's and 2's complement on a number circle
Ranges for n bits
| Representation | Range | Zeros | 4-bit range |
|---|---|---|---|
| Unsigned | 0 to 2n - 1 | one | 0 to 15 |
| Sign-magnitude | -(2n-1 - 1) to +(2n-1 - 1) | two (+0, −0) | −7 to +7 |
| 1's complement | -(2n-1 - 1) to +(2n-1 - 1) | two (0000, 1111) | −7 to +7 |
| 2's complement | -2n-1 to +(2n-1 - 1) | one | −8 to +7 |
Why every computer uses 2's complement:
- there is only one zero, so equality tests are simple;
- addition and subtraction use the same adder with no sign checks;
- it has one extra negative value (-2n-1, for example 10000000 = −128 in 8 bits).
Shortcut for the 2's complement: copy bits from the right up to and including the first 1, then invert all the remaining bits. For 00101100: keep "100", invert "00101" to "11010", giving 11010100.
Reading a 2's complement number
The sign bit has a negative weight of -2n-1:
For example 11010011 = -128 + 64 + 16 + 2 + 1 = -45.
Sign extension: to widen a 2's complement number, copy the sign bit into the new positions. −45 in 16 bits is 11111111 11010011.
2. Complements in Any Base
For a base-r number N with n integer digits there are two complements:
| Base | Complements | Example N | (r-1)'s | r's |
|---|---|---|---|---|
| 10 | 9's, 10's | 52520 | 47479 | 47480 |
| 2 | 1's, 2's | 1011000 | 0100111 | 0101000 |
The 9's complement subtracts each digit from 9; the 1's complement subtracts each bit from 1, which is just inversion.
3. Subtraction by Adding the Complement
To compute M - N with r's complements:
- Add M to the r's complement of N.
- If M ≥ N there is an end carry: discard it, and the rest is the answer.
- If M < N there is no end carry: the answer is negative; take the r's complement of the sum and put a minus sign in front.
With (r-1)'s complements the rule changes in step 2: the end carry is added back to the LSB (end-around carry).
Compute 72532 - 3250 and 3250 - 72532.
- 10's complement of 03250 is 96750. 72532 + 96750 = 169282. End carry 1 is discarded: 69282.
- 10's complement of 72532 is 27468. 03250 + 27468 = 30718. No end carry, so the answer is -(10's comp of 30718) = -69282.
With X = 1010100 (84) and Y = 1000011 (67), find X - Y and Y - X.
- 2's complement, X − Y: 2's complement of Y is 0111101. 1010100 + 0111101 = 1 0010001. Discard the carry: 0010001 = 17.
- 2's complement, Y − X: 2's complement of X is 0101100. 1000011 + 0101100 = 1101111, no carry, so the answer is -(0010001) = -17.
- 1's complement, X − Y: 1's complement of Y is 0111100. 1010100 + 0111100 = 1 0010000. End-around carry: 0010000 + 1 = 0010001 = 17.
| Feature | 1's complement | 2's complement |
|---|---|---|
| Forming the complement | invert bits | invert bits and add 1 |
| End carry in subtraction | added back (end-around) | discarded |
| Zero | two forms | one form |
| Hardware | slightly simpler complementer, extra add cycle | used by all modern CPUs |
4. Signed Addition and Overflow
In 2's complement the adder ignores signs: add all bits including the sign bits and discard the carry out of the sign position.
Overflow happens when the true result does not fit in n bits. It is possible only when both operands have the same sign, and it shows up as a result with the wrong sign. In hardware it is detected by comparing the carry into the sign bit with the carry out of it:
Add +70 + 80 and (-70) + (-80) in 8-bit 2's complement.
- 01000110 + 01010000 = 10010110. Carry into sign = 1, carry out = 0, so V = 1. The result reads as −106: overflow (150 > 127).
- 10111010 + 10110000 = 1 01101010. Carry into sign = 0, carry out = 1, so V = 1. The result reads as +106: overflow (−150 < −128).
- By contrast +70 + (-80) = 01000110 + 10110000 = 11110110 = -10, V = 0, correct.
The same adder in Verilog
Verilog describes hardware in text. This module adds two 8-bit numbers and raises overflow exactly when both inputs have the same sign and the sum's sign differs. The testbench (tb) feeds it the cases above and prints the results.
// 8-bit two's complement adder with overflow detection
module add8_ovf (
input [7:0] a,
input [7:0] b,
output [7:0] sum,
output cout,
output overflow
);
assign {cout, sum} = a + b;
// overflow: both operands have the same sign and the sum's sign differs
assign overflow = (a[7] == b[7]) && (sum[7] != a[7]);
endmodule
module tb;
reg [7:0] a, b;
wire [7:0] s;
wire c, v;
add8_ovf dut (.a(a), .b(b), .sum(s), .cout(c), .overflow(v));
task show;
begin
#1 $display("%4d + %4d = %4d carry=%b V=%b", $signed(a), $signed(b), $signed(s), c, v);
end
endtask
initial begin
a = 8'd70; b = 8'd80; show;
a = -8'sd70; b = -8'sd80; show;
a = 8'd70; b = -8'sd80; show;
a = -8'sd45; b = 8'd45; show;
end
endmoduleSimulating with Icarus Verilog (iverilog then vvp) prints:
70 + 80 = -106 carry=0 V=1
-70 + -80 = 106 carry=1 V=1
70 + -80 = -10 carry=0 V=0
-45 + 45 = 0 carry=1 V=0Notice the last line: there is a carry out, but no overflow. In 2's complement the carry and the overflow are different signals.
5. Fixed-Point Numbers
A fixed-point number has the binary point at a fixed position agreed in advance. The hardware stores only an integer; the programmer remembers where the point is.
- Integer representation: point after the LSB (the usual case).
- Fraction representation: point just after the sign bit, so values lie in [-1, 1).
- Q format Qm.n: m integer bits and n fraction bits.
For an 8-bit word with 4 integer and 4 fraction bits, 0101.1100 = 4 + 1 + 0.5 + 0.25 = 5.75. Its negative in 2's complement is 1010.0100, which reads as -8 + 2 + 0.25 = -5.75.
| Pros of fixed point | Cons of fixed point |
|---|---|
| Uses the ordinary integer adder and multiplier | Small, fixed range |
| Fast and cheap; used in DSPs and microcontrollers | Programmer must track the point and scale values |
| Exact for values that are multiples of 2-n | Very large and very small values cannot coexist |
6. Floating-Point Numbers and IEEE 754
A floating-point number is stored like scientific notation, N = ± M × rE: a sign, a mantissa (significand, the digits) and an exponent (where the point goes). This trades exact precision for a huge range.
A binary number is normalized when the mantissa has a single 1 before the point: 1101.101 = 1.101101 × 23. Since the leading bit of a normalized number is always 1, IEEE 754 does not store it (the hidden bit).
IEEE 754 formats
IEEE 754 single and double precision fields
| Format | Total | Sign | Exponent | Fraction | Bias |
|---|---|---|---|---|---|
| Single (float) | 32 | 1 | 8 | 23 | 127 |
| Double (double) | 64 | 1 | 11 | 52 | 1023 |
The exponent is stored with a bias (excess code) so that it is never negative and numbers can be compared like integers. A normalized value is
Special values
| Exponent field | Fraction | Meaning |
|---|---|---|
| all 0s | 0 | ±0 |
| all 0s | not 0 | denormal: (-1)S × 0.F × 2-126 |
| 1 to 254 | any | normalized number |
| all 1s (255) | 0 | ±infinity |
| all 1s (255) | not 0 | NaN (not a number), such as 0/0 |
Represent -13.625 in IEEE 754 single precision; give the hex pattern.
- Sign: negative, so S = 1.
- Binary: 13.625 = 1101.101.
- Normalize: 1101.101 = 1.101101 × 23.
- Biased exponent: 3 + 127 = 130 = 10000010.
- Fraction (drop the hidden 1, pad to 23 bits): 10110100000000000000000.
- Pattern: 1 10000010 10110100000000000000000; in groups of 4: 1100 0001 0101 1010 0000 0000 0000 0000.
Represent 0.15625 in single precision.
- 0.15625 = 0.001012 = 1.01 × 2-3.
- S = 0, exponent = -3 + 127 = 124 = 01111100, fraction = 0100ldots0.
- 0 01111100 01000000000000000000000 = 0011 1110 0010 0000 ...
Which decimal number is stored as (42F60000)16?
- Binary: 0100 0010 1111 0110 0000 ldots
- S = 0; exponent bits 10000101 = 133, so the true exponent is 133 - 127 = 6.
- Fraction bits 1110110 0ldots, so the mantissa is 1.111011.
- 1.111011 × 26 = 1111011 = 123.
Fixed point vs floating point
| Feature | Fixed point | Floating point |
|---|---|---|
| Point position | fixed by convention | stored in the exponent |
| Range | small | very large (≈ ± 3.4 × 1038 for float) |
| Precision | uniform steps | relative: about 7 decimal digits (float), 16 (double) |
| Hardware | integer ALU | separate floating-point unit (FPU) |
| Speed and cost | fast, cheap | slower, more hardware |
Key points to remember
- Sign-magnitude and 1's complement have two zeros; 2's complement has one and range -2n-1 to 2n-1 - 1.
- r's complement = rn - N; (r-1)'s complement = rn - 1 - N.
- Subtraction: add the complement; discard the end carry (2's) or add it back (1's). No carry means a negative result.
- Overflow V = Cn oplus Cn-1; possible only when both operands have the same sign.
- IEEE 754 single: 1 + 8 + 23 bits, bias 127, hidden leading 1. Double: 1 + 11 + 52, bias 1023.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Represent −45 in sign-magnitude, 1's complement and 2's complement using 8 bits. Why is 2's complement preferred? (5 Marks)
- Write: the three patterns, the range table, and the one-zero / same-adder argument.
- Q2. Perform X - Y and Y - X for X = 1010100, Y = 1000011 using (a) 2's complement (b) 1's complement. (5 Marks)
- Q3. What is overflow? How is it detected in 2's complement addition? Give an example. (5 Marks)
- Write: the same-sign rule, V = Cn oplus Cn-1, and the 70 + 80 example in 8 bits.
- Q4. Represent −13.625 in IEEE 754 single precision. Explain the bias and the hidden bit. (5 Marks)
- Q5. Differentiate between fixed-point and floating-point representation. (5 Marks)