Draft preview. Lessons are being written and reviewed.

Digital Design / Module 1: Numbers and logic

1. Binary, hex and two's complement

Read and convert the number formats every waveform uses.

Lecture coming soon

See it move

The idea

Wires carry only two voltage levels, so everything a chip handles is a pattern of bits. The pattern has no meaning of its own. The same eight bits, 11110011, are 243 if you read them as unsigned and -13 if you read them as two's complement. The hardware doesn't know which you intended. You do, and your design has to treat the bits consistently.

Reading the formats

Each binary position is worth a power of two. With N bits you get 2^N patterns. Hex is shorthand: one hex digit is exactly four bits, which is why waveforms and memory dumps use it. 1111 0011 is F3.

Two's complement gives the top bit a negative weight. In 8 bits, that top bit is worth -128 rather than +128, so the signed range is -128 to +127. In general it is -2^(N-1) to 2^(N-1)-1. There is one more negative value than positive, because zero uses up a non-negative pattern.

Why chips use two's complement

To negate a number, invert every bit and add one. The real prize is that subtraction becomes addition: A - B equals A + ~B + 1. One adder handles signed and unsigned values, and zero has only one pattern. The older sign-magnitude format has both +0 and -0, and it needs separate logic to subtract.

Worked example: -13 in 8 bits

  • Start with +13: 00001101
  • Invert every bit: 11110010
  • Add one: 11110011, which is F3 in hex

Check it by adding the weights: -128 + 64 + 32 + 16 + 2 + 1 = -13.

Common mistake

Widening a signed number by padding with zeros. -13 in 8 bits is F3, but in 16 bits it is FFF3, not 00F3. Copy the sign bit into the new positions. This is called sign extension. In Verilog, mixing signed and unsigned operands quietly makes the whole expression unsigned, which gives the same kind of bug.

Key ideas

  • An N-bit value holds 2^N patterns; meaning comes from how we interpret them.
  • Two's complement negates by inverting and adding one.
  • An N-bit signed range is -2^(N-1) to 2^(N-1)-1.

Your exercise

Convert -13 to 8-bit two's complement and to hex.

Interview questions

coreWhat is the range of a 6-bit signed number?

coreWhy does two's complement dominate?

basicHow many different patterns can a 10-bit bus carry, and what range does it cover when read as unsigned?

basicWrite -1 as an 8-bit and as a 16-bit two's complement value, in hex.

coreThe byte 0xB6 appears on a bus. What is it in binary, as an unsigned number and as an 8-bit signed number?

coreApply the invert-and-add-one rule to -128 in 8 bits. What do you get, and why?

coreA designer widens a signed 8-bit sample to 16 bits as shown. The sample holds -13. What does w hold, and how do you fix it?

wire [7:0]  a = 8'hF3;              // -13
wire [15:0] w = a;                   // zero-padded
wire [15:0] s = {{8{a[7]}}, a};      // sign-extended

coreWhat is the smallest two's complement width that can hold every integer from -100 to +100?

advancedIn this Verilog, sa holds -3 and ub holds 5. Is sa < ub true or false, and why?

reg signed [7:0] sa = -8'sd3;
reg        [7:0] ub = 8'd5;
// what does (sa < ub) give?

advancedYou must narrow a 16-bit signed value x to 8 bits and raise a flag when it does not fit. How do you test whether it fits?

wire fits = (x[15:7] == 9'h000) ||
            (x[15:7] == 9'h1FF);
wire [7:0] x8 = x[7:0];

Practise these 10 questions with animations