Lesson 2 +35 XP

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.

RepresentationHow -N is formed+45 in 8 bits−45 in 8 bits
Sign-magnitudesign bit 1, magnitude unchanged0010110110101101
1's complementinvert every bit of +N0010110111010010
2's complementinvert every bit, then add 10010110111010011

Positive numbers look the same in all three. Only negative numbers differ.

Sign-magnitude, 1's and 2's complement on a number circleSign-magnitude, 1's and 2's complement on a number circle

Ranges for n bits

RepresentationRangeZeros4-bit range
Unsigned0 to 2n - 1one0 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).
💡 Tip

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:

(bn-1 bn-2 ldots b0)2's = -bn-1 2n-1 + ∑i=0n-2 bi 2i

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:

diminished radix ((r-1)'s) complement = (rn - 1) - N
radix (r's) complement = rn - N = (r - 1)'s complement + 1
BaseComplementsExample N(r-1)'sr's
109's, 10's525204747947480
21's, 2's101100001001110101000

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:

  1. Add M to the r's complement of N.
  2. If M ≥ N there is an end carry: discard it, and the rest is the answer.
  3. 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).

SolvedExample 1 · Decimal subtraction with 10's complement

Compute 72532 - 3250 and 3250 - 72532.

  1. 10's complement of 03250 is 96750. 72532 + 96750 = 169282. End carry 1 is discarded: 69282.
  2. 10's complement of 72532 is 27468. 03250 + 27468 = 30718. No end carry, so the answer is -(10's comp of 30718) = -69282.
Answer69282 and −69282
SolvedExample 2 · Binary subtraction with 2's and 1's complement

With X = 1010100 (84) and Y = 1000011 (67), find X - Y and Y - X.

  1. 2's complement, X − Y: 2's complement of Y is 0111101. 1010100 + 0111101 = 1 0010001. Discard the carry: 0010001 = 17.
  2. 2's complement, Y − X: 2's complement of X is 0101100. 1000011 + 0101100 = 1101111, no carry, so the answer is -(0010001) = -17.
  3. 1's complement, X − Y: 1's complement of Y is 0111100. 1010100 + 0111100 = 1 0010000. End-around carry: 0010000 + 1 = 0010001 = 17.
AnswerX - Y = 0010001 (+17), Y - X = -0010001 (−17)
Feature1's complement2's complement
Forming the complementinvert bitsinvert bits and add 1
End carry in subtractionadded back (end-around)discarded
Zerotwo formsone form
Hardwareslightly simpler complementer, extra add cycleused 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:

V = Cn oplus Cn-1
SolvedExample 3 · Detecting overflow in 8 bits

Add +70 + 80 and (-70) + (-80) in 8-bit 2's complement.

  1. 01000110 + 01010000 = 10010110. Carry into sign = 1, carry out = 0, so V = 1. The result reads as −106: overflow (150 > 127).
  2. 10111010 + 10110000 = 1 01101010. Carry into sign = 0, carry out = 1, so V = 1. The result reads as +106: overflow (−150 < −128).
  3. By contrast +70 + (-80) = 01000110 + 10110000 = 11110110 = -10, V = 0, correct.
Answerboth sums overflow; mixed-sign sums never overflow

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.

VERILOG
// 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
endmodule

Simulating with Icarus Verilog (iverilog then vvp) prints:

OUTPUT
  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=0

Notice 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 pointCons of fixed point
Uses the ordinary integer adder and multiplierSmall, fixed range
Fast and cheap; used in DSPs and microcontrollersProgrammer must track the point and scale values
Exact for values that are multiples of 2-nVery 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 fieldsIEEE 754 single and double precision fields

FormatTotalSignExponentFractionBias
Single (float)321823127
Double (double)64111521023

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

N = (-1)S × 1.F × 2^{E - bias}

Special values

Exponent fieldFractionMeaning
all 0s0±0
all 0snot 0denormal: (-1)S × 0.F × 2-126
1 to 254anynormalized number
all 1s (255)0±infinity
all 1s (255)not 0NaN (not a number), such as 0/0
SolvedExample 4 · Encoding −13.625 in single precision

Represent -13.625 in IEEE 754 single precision; give the hex pattern.

  1. Sign: negative, so S = 1.
  2. Binary: 13.625 = 1101.101.
  3. Normalize: 1101.101 = 1.101101 × 23.
  4. Biased exponent: 3 + 127 = 130 = 10000010.
  5. Fraction (drop the hidden 1, pad to 23 bits): 10110100000000000000000.
  6. Pattern: 1 10000010 10110100000000000000000; in groups of 4: 1100 0001 0101 1010 0000 0000 0000 0000.
Answer(C15A0000)16
SolvedExample 5 · Encoding a small fraction

Represent 0.15625 in single precision.

  1. 0.15625 = 0.001012 = 1.01 × 2-3.
  2. S = 0, exponent = -3 + 127 = 124 = 01111100, fraction = 0100ldots0.
  3. 0 01111100 01000000000000000000000 = 0011 1110 0010 0000 ...
Answer(3E200000)16
SolvedExample 6 · Decoding a pattern

Which decimal number is stored as (42F60000)16?

  1. Binary: 0100 0010 1111 0110 0000 ldots
  2. S = 0; exponent bits 10000101 = 133, so the true exponent is 133 - 127 = 6.
  3. Fraction bits 1110110 0ldots, so the mantissa is 1.111011.
  4. 1.111011 × 26 = 1111011 = 123.
Answer+123.0

Fixed point vs floating point

FeatureFixed pointFloating point
Point positionfixed by conventionstored in the exponent
Rangesmallvery large (≈ ± 3.4 × 1038 for float)
Precisionuniform stepsrelative: about 7 decimal digits (float), 16 (double)
Hardwareinteger ALUseparate floating-point unit (FPU)
Speed and costfast, cheapslower, more hardware
💡 Tip

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.
🎯 END-TERM EXAM PREP

📝 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)