Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 37 ยท Modules and Data Types

Verilog Numbers, Literals and Sizes

Master Verilog number literals: size, base and value syntax, binary, hex, decimal, x and z digits, underscores, signed literals, truncation and extension, with runnable C checks.

Numbers in Verilog carry a size in bits, not just a value. A typo in a literal's size or base is one of the quietest ways to create a bug. This lesson covers the literal syntax, how sizes interact with the width of the target, and what happens when a value is too big or too small.

The literal format

TEXT
<size>'<base><value>
  • size: number of bits (decimal). Optional.
  • base: b binary, o octal, d decimal, h hex (case-insensitive). Add s for signed ('sd).
  • value: digits in that base. _ may appear for readability. x and z are valid digits in b, o and h.
Literal Bits Value
4'b1010 4 10
8'hFF 8 255
8'd200 8 200
12'o777 12 511
16'hDEAD 16 57005
8'b1010_0101 8 165 (underscore is ignored)
4'bx01z 4 unknown, 0, 1, high impedance
42 32 (at least) 42, unsized decimal, signed
'hFF 32 255, unsized

Always size your literals in hardware code. An unsized '1 or 0 in an assignment to an 8-bit signal can behave in surprising ways in some expressions, because unsized numbers are at least 32 bits wide.

Padding and truncation rules

If the value is narrower than the size, it is zero-extended on the left (or extended with x or z if the leading digit is x or z). If it is wider, it is truncated, dropping the high bits.

VERILOG
reg [7:0] a;
a = 4'b1010;       // 8'b0000_1010  (zero-extended)
a = 12'hABC;       // 8'hBC         (truncated; high nibble lost)
a = 4'bx;          // 8'bxxxx_xxxx  (x extends)

A signed literal is sign-extended only if the target and the expression are signed. Otherwise it is padded with zeros. -8'sd3 is a signed 8-bit value of -3 (8'hFD).

Fill literals in SystemVerilog

SystemVerilog adds '0, '1, 'x and 'z, which fill the whole target width with that value regardless of its size:

VERILOG
logic [31:0] all_ones = '1;   // 32'hFFFF_FFFF
logic [15:0] zeros    = '0;

In plain Verilog, write {16{1'b1}} (replication) or ~16'b0 instead.

Reading and writing numbers with $display

VERILOG
$display("%b %h %d %o", 8'hA5, 8'hA5, 8'hA5, 8'hA5);
// prints: 10100101 a5 165 245
$display("%d", 4'b1010);   // 10

I am showing the output only because the conversions are exact: 0xA5 is 10100101 in binary, 165 in decimal and 245 in octal.

Stand-in: size and truncation in C

C has no sized literals, but masks give the same effect. This stand-in (not Verilog) writes helper functions to fit a value to a given number of bits (truncate or zero-extend) and to sign-extend, so you can check the rules above.

C Stand-in: truncate, extend and sign-extend (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The last line shows a common surprise: 8'd200 is a perfectly good unsigned value, but read as two's complement it is -56.

Base choice and readability

  • Use hex for addresses, opcodes and data constants (32'hFFFF_0000).
  • Use binary for small control patterns where every bit matters (3'b101).
  • Use decimal for counts and limits (8'd100).
  • Underscores group bits: 16'b1010_0000_1111_0101.

Common mistakes

  • Writing 8'hFF as 8'FF (the base letter is missing) or 8h'FF (the apostrophe is in the wrong place).
  • Using a value too large for the size, such as 4'd20; it silently truncates to 4'd4 (a warning in most tools).
  • Comparing against an unsized number in a way that makes the expression 32 bits wide.
  • Forgetting that x and z digits are not allowed in decimal literals (except as a whole value like 8'dx).

A tiny practice

What is {4'hA, 4'h5}? It concatenates two nibbles into 8'hA5. What is 4'b1100 & 4'b1010? Each bit pair is ANDed: 4'b1000. Use the C stand-in with 0xC & 0xA to confirm.

C Stand-in: check the practice answers (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many bits wide is 8'hF3?

    The number before the apostrophe is the size in bits.
  2. 2. What happens when you assign 12'hABC to an 8-bit reg?

    Verilog truncates on the left, keeping the least significant bits.
  3. 3. What is 4'b1x01?

    x is a valid digit in binary literals and marks an unknown bit.
  4. 4. What base does 8'o17 use?

    The letter o selects octal; 8'o17 is decimal 15.

Summary

  • Literals look like size'base value; always size hardware constants.
  • Short values are zero-extended; long values are truncated on the left.
  • x and z are valid digits in binary, octal and hex; underscores improve readability.
  • Signedness is explicit ('sd, signed), and the same bits can mean 200 or -56.
  • Use hex for data and addresses, binary for control bits, decimal for counts.

Next: Vectors, Bit-Selects and Concatenation