RTL Design / Module 2: Digital Foundations
Lesson 2.1

Bits, buses & numbers in hardware (signed/unsigned, overflow)

Hardware does not store “integers” the way Python does. It stores bits — wires that are 0 or 1 — grouped into buses. Signed vs unsigned is a interpretation of those bits. Overflow is what happens when the math does not fit the width.

Core idea

Width is part of the type. An 8-bit unsigned bus holds 0…255. Add 1 to 255 and you wrap to 0 unless you widen the result.

1

Bits, vectors, and buses

A single wire is 1 bit. A vector (bus) is [MSB:LSB] — in Verilog usually [7:0] means bit 7 is the most significant.

  1. Count the width: [7:0] → 8 wires (7 down to 0 inclusive).
  2. MSB has the largest weight: bit 7 = 2⁷, bit 0 = 2⁰.
  3. Declare once; drive with assign or inside always blocks.
8-bit bus

MSB = most significant bit (highest weight). LSB = least significant bit (weight 1).

Verilog syntax — declaring widths

SyntaxMeaning
wire a;1-bit net (default)
wire [7:0] data;8-bit bus, bit 7 down to 0
reg [3:0] nibble;4-bit storage (in always blocks)
8'b1010_1100Sized binary literal (underscores optional)
8'hACSame value in hex
8'd172Same value in decimal
bus_declare.v
module bus_declare;
  wire       flag;          // 1 bit
  wire [7:0] data;          // 8-bit bus
  wire [15:0] addr;         // 16-bit address

  // Part-select and concatenation
  wire [3:0] high_nibble = data[7:4];
  wire [3:0] low_nibble  = data[3:0];
  wire [15:0] word = {data, data}; // replicate/concat
endmodule

Syntax tip: {a,b} concatenates. {4{1'b1}} replicates. Width of the result is the sum of widths — mismatches cause silent padding/truncation bugs.

2

Unsigned vs signed — same bits, different meaning

The pattern 1111_1111 is 255 unsigned or −1 in 8-bit two’s complement signed. Hardware stores bits; signed tells tools how to interpret arithmetic and comparisons.

bits-buses-numbers diagram 2

Two’s complement: negate by invert-all-bits then add 1.

signed_demo.v
module signed_demo (
  input  wire signed [7:0] a,
  input  wire signed [7:0] b,
  output wire signed [7:0] sum_wrap,  // may overflow
  output wire signed [8:0] sum_wide   // safe width
);
  assign sum_wrap = a + b;           // result truncated to 8 bits
  assign sum_wide = a + b;           // Verilog extends for wider LHS
endmodule
Trap

Mixing signed and unsigned in one expression often causes unexpected zero/sign extension. Keep types consistent, or cast explicitly with $signed() / $unsigned() in SV.

Worked example — negate +5 in 4-bit two's complement

Follow the bit pattern step by step. Goal: compute −5 from +5.

  1. Write +5 in 4 bits: 4'b0101 (MSB=0 → positive).
  2. Invert every bit: 4'b1010.
  3. Add 1: 4'b1010 + 4'b0001 = 4'b1011 → that is −5 signed.
  4. Check: 4'sb1011 + 4'sb0101 = 4'b0000 (wraps to 0 in 4 bits) ✓
Two's complement negation steps

Negation recipe: ~x + 1. MSB flips from 0→1 when going positive to negative.

Interview angle

“What is 8'hFF as signed?” → −1. “As unsigned?” → 255. Same bit pattern — always state the width and signedness.

Truncation vs extension — width changes matter

When you assign a wide bus to a narrow one, truncation drops MSBs silently. When you widen, Verilog zero-extends unsigned values and sign-extends signed values — unless you concatenate explicitly.

OperationVerilog patternEffect
Truncatewire [3:0] lo = wide[3:0];Keep LSBs, discard MSBs
Zero-extend{4'b0000, byte} or 8'h00, bytePad MSBs with 0 (unsigned widen)
Sign-extend{{4{byte[7]}}, byte}Replicate MSB (signed widen)
Explicit cast (SV)$signed(wide) / $unsigned(s)Force interpretation before math
trunc_extend.v
module trunc_extend (
  input  wire signed [7:0] s8,   // e.g. 8'sb1111_1111 = -1
  input  wire        [7:0] u8,   // e.g. 8'hFF = 255
  output wire signed [3:0] s_trunc,
  output wire signed [15:0] s_sign_ext,
  output wire        [15:0] u_zero_ext
);
  assign s_trunc     = s8;              // truncates to [3:0] → 4'b1111 (-1)
  assign s_sign_ext  = s8;              // sign-extends to 16 bits → 16'hFFFF
  assign u_zero_ext  = u8;              // zero-extends → 16'h00FF
  // Explicit (portable):
  // assign s_sign_ext = {{8{s8[7]}}, s8};
  // assign u_zero_ext = {8'h00, u8};
endmodule
Trap

Comparing 8'hFF (unsigned 255) to 8'sd-1 can surprise you — tools may widen unsigned with zeros while sign-extending the signed value. Cast before compare.

▶ Try trunc_extend with s8=−1 and u8=255

3

Overflow — when the answer does not fit

Unsigned wrap: 8'd255 + 8'd1 = 8'd0. Signed wrap: 8'sd127 + 8'sd1 = −128. Both are “correct” for modular arithmetic — and disastrous if you expected math like a calculator.

Step-by-step unsigned wrap: (1) Add binary: 1111_1111 + 0000_0001 = 1_0000_0000 (9 bits). (2) Keep only 8 LSBs → 0000_0000. (3) Carry-out bit 8 = 1 → that is your overflow flag.

Unsigned wrap (overflow) at 255 → 0. Two's complement wrap at +127 → -128.

overflow_flag.v
// Detect unsigned overflow on 8-bit add
module overflow_flag (
  input  wire [7:0] a, b,
  output wire [7:0] sum,
  output wire       overflow
);
  wire [8:0] full = {1'b0, a} + {1'b0, b};
  assign sum      = full[7:0];
  assign overflow = full[8];   // carry-out = unsigned overflow
endmodule

▶ Simulate overflow_flag

OperationSafe pattern
Add N-bit + N-bitResult width N+1 (or keep N and track carry/overflow)
Multiply N×NResult width 2N
Shift leftBits fall off MSB — that is intentional loss unless you widen first
4

Operators you will use constantly

OperatorRoleNote
+ - * / %Arithmetic/ % costly in hardware — prefer shifts when power-of-2
& | ^ ~BitwiseBit-by-bit logic
&& || !LogicalReduce to true/false (1-bit sense)
== !=EqualityUse === in TB for X/Z awareness
<< >>ShiftLogical; arithmetic >>> in SV for signed
{ } {n{}}Concat / replicateBuild buses from pieces
💡 Analogy

A bus is a multi-lane highway. Overflow is cars spilling onto a road that only has N lanes — they wrap around unless you built an exit ramp (wider result).

5

Interview / lab checklist

  • I can read [7:0] and name MSB/LSB
  • I can convert hex ↔ binary for small widths
  • I know why 8-bit 255+1 becomes 0
  • I widen adders when I need a true mathematical sum
  • I can negate a signed value by invert + 1
  • I know truncation drops MSBs; extension pads them

🎯 What to remember

  • Bits are physical; signed/unsigned is interpretation.
  • Always think about width before arithmetic.
  • Overflow wraps unless you detect it or widen.
  • Next: combinational vs sequential — the mental model of RTL.
Advertisement