Write unambiguous constants, manipulate bits, and avoid width and signedness pitfalls.
A Verilog constant is easier to read with an explicit width and base. The form width'basevalue identifies the requested bits.
8'b1010_01108'hA68'd16612'o245
The first three forms have eight bits; the last has twelve. b, o, d and h mean binary, octal, decimal and hexadecimal. Underscores improve readability.
Size and assemble bits
An unsized constant such as 5 follows implicit width and signedness rules. In RTL, 4'd5 or 8'd5 expresses the format more clearly.
[7:0] describes eight bits, w_byte[0] selects one and w_byte[7:4] selects a contiguous part. Ascending ranges exist too; read the declaration before interpreting indexes.
w_header occupies the most significant positions. Repeating 2'b10 four times gives 8'b1010_1010. To extend a signed value, repeat its sign bit.
Choose the operation
Family
Examples
Result
arithmetic
+, -, *
calculation on numbers
bitwise
&, |, ^, ~
independent operation on each bit
logical
&&, ||, !
true, false, or unknown condition
comparison
<, >=, ==, !=
one-bit result
reduction
&bus, |bus, ^bus
reduction of a vector to one bit
shift
<<, >>
movement of bits
conditional
condition ? a : b
choice between two values
a & b operates bit by bit; a && b combines logical conditions. Reductions such as ^bus produce one bit from the whole vector.
Simulation represents 0, 1, x and z. x indicates an unknown or conflict, while z indicates high impedance. Find an x's cause before replacing it: it may expose missing reset or an uncovered path.
Retain the numeric result
Operand widths and context affect calculation size. Explicitly widen these inputs to nine bits to retain addition carry: