Unsigned, two's complement, Gray code, BCD, Excess-3, parity and Hamming: choose the right encoding for the circuit.
Before calculating with a bus, decide what its bits represent. On four bits, 1000 means unsigned , two's-complement , BCD digit or a position in a Gray sequence. Encoding is part of the specification.
8
-8
8
Represent integers
An unsigned integer runs from zero to the maximum allowed by its width:
Width
Range
4 bits
0 to 15
8 bits
0 to 255
16 bits
0 to 65,535
The VHDL type unsigned makes that choice explicit:
signal counter : unsigned(7 downto 0);counter <= counter + 1;
On eight bits, incrementing 255 returns to 0. The calculation wraps modulo 256 when the result keeps that width.
Two's complement represents signed integers. Its range on N bits is:
-2^(N-1) to +2^(N-1)-1
Width
Signed range
4 bits
-8 to +7
8 bits
-128 to +127
16 bits
-32,768 to +32,767
To form the opposite, invert the bits and add 1 while retaining the chosen width:
+3 = 0011NOT = 1100+1 = 1101 therefore -3
In VHDL, signed identifies this interpretation and the role of the most significant bit:
signal a, b, y : signed(7 downto 0);y <= a - b;
Widen before calculating
An operation may need an extra bit to retain its result. Add zeros on the left of an unsigned value:
Incorrect extension or mixing std_logic_vector, signed and unsigned can change the value without making the bit pattern look suspicious.
Encode positions and digits
Gray code changes only one bit between consecutive positions:
Decimal
Binary
Gray
0
000
000
1
001
001
2
010
011
3
011
010
4
100
110
5
101
111
6
110
101
7
111
100
This reduces the risk of reading an inconsistent intermediate value during a transition. Uses include rotary encoders, position measurements and asynchronous FIFO pointers crossing clock domains. Gray coding belongs within a clock-domain-crossing strategy; it does not replace synchronization by itself.
BCD (binary coded decimal) allocates four bits to each decimal digit:
12₁₀ = 0001 0010 in BCD12₁₀ = 0000 1100 in pure binary
It is less compact than binary but convenient for seven-segment displays, decimal input, watches and displayed counters. Patterns 1010 through 1111 are invalid BCD digits. In 8421 BCD, positions have these weights:
Bit
Weight
b3
8
b2
4
b1
2
b0
1
Thus 0101 represents 4 + 1 = 5. Excess-3 also uses four bits per digit, encoding the digit plus 3:
Digit
+3
Excess-3
0
3
0011
1
4
0100
5
8
1000
9
12
1100
It saves no space compared with BCD. It demonstrates how a convention changes the meaning of a binary word.
Detect and correct errors
With even parity, the total number of 1 bits, including parity, must be even. Their XOR is then 0:
This detects changes to an odd number of bits. It neither locates the error nor corrects it alone.
Hamming (7,4) adds three parity bits to four data bits. The classic convention assigns its seven positions as follows:
Position
1
2
3
4
5
6
7
Role
P1
P2
D1
P4
D2
D3
D4
During decoding, parity checks form a syndrome. A value of 0 means no single-bit error was detected. Otherwise, assuming a single-bit error, the syndrome identifies the bit to flip.
Hamming (7,4) cannot correct two errors or always distinguish them correctly. Adding global parity provides SECDED: single-error correction and double-error detection.
Choose for the circuit
Need
Recommended encoding
Counter, address, size
unsigned
Arithmetic with positive and negative values
signed two's complement
Position read during a transition
Gray
Direct decimal display
BCD
Simple error detection
Parity
Single-bit error correction
Hamming
Masks, flags, frames
std_logic_vector
Make the encoding visible in types, names and useful comments. Conversions then become explicit and simulation values easier to interpret.