Concatenation, Replication, Ternary, Bitwise and Reduction
Build vectors with braces, choose with the ternary, and combine bits with bitwise and reduction operators. Know what each one costs in gates.
You Will Be Able To
- Predict the width and the bits of a concatenation or a replication.
- Explain why an expression inside braces can lose a carry.
- Choose between a nested ternary and a
caseby the logic each one builds. - Write a sign extension and a parity bit, and predict their cells and levels.
- Lesson
- R10.1
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
A status word carried the sum of two bytes. For 200 plus 100 it reported 44. The adder was fine. The sum was written inside a pair of braces, and the braces dropped the carry.
The sum in a 9-bit context
logic [7:0] a, b; // 200 and 100
logic [8:0] sum;
assign sum = a + b;
Icarus 12 prints 300. The 9-bit target widens the add, so the carry has a place.
The same sum inside braces
logic [7:0] a, b; // 200 and 100
logic [8:0] sum;
assign sum = {a + b};
Icarus 12 prints 44. Inside braces the add is 8 bits wide. 300 does not fit, and 300 minus 256 is 44.
Braces Are Wiring
Braces join vectors end to end. This is concatenation. The first operand becomes the upper bits. {n{x}} repeats x n times. This is replication.
y = {a, b, {2{a}}}. Nothing is computed.- I/O
- Data
- Join
alands in the top four bits andbin the next four. The order in the braces is the order in the vector. - Repeat
{2{a}}isatwice, 8 bits. Each wire ofanow feeds three places. - Cost
Synthesize reports 0 cells and 0 levels. A byte swap and a sign extension are also 0 cells.
Read the diagram as text
- a[3:0]: 4 bits
- b[3:0]: 4 bits
- y[15:12]: a
- y[11:8]: b
- y[7:0]: a, then a again
- y[15:0]: 0 cells, 0 levels
- a[3:0] → y[15:12]: wires
- b[3:0] → y[11:8]: wires
- a[3:0] → y[7:0]: two copies
- y[15:12] → y[15:0]
- y[11:8] → y[15:0]
- y[7:0] → y[15:0]
Measured on this flow: {a, b, {2{a}}}, {{4{d[3]}}, d} and {d[7:0], d[15:8]} each build 0 cells.
Two rules follow from "braces are wiring". Every operand must have a known width. And every operand keeps its own width: the target of the assignment does not reach inside the braces.
That second rule explains the lost carry. In sum = a + b the 9-bit target widens the add to 9 bits. In sum = {a + b} the add sits inside braces. It stays 8 bits wide, and the result is then padded with a 0.
| Expression, 8-bit target | Value | Why |
|---|---|---|
{4'hA, 2'b11} | 2b | six bits, placed at the bottom, zeros above |
{{4{d[3]}}, d} with d = 4'b1010 | fa | four copies of the sign bit: a sign extension |
{4'(d[3]), d} with d = 4'b1010 | 1a | a size cast pads with zeros: not a sign extension |
{4{d[3]}} with d = 4'b1010 | 0f | four bits only, zeros above |
Measured on Icarus 12 and Verilator 5.052; both print the same values.
The Ternary Is a Mux
sel ? a : b picks a when sel is 1 and b when it is 0. In gates it is one 2-to-1 mux per bit.
A ternary inside a ternary is a priority chain. In c0 ? a : c1 ? b : c2 ? c : d the first true condition wins. c0 beats c1 even when both are 1.
| 8-bit design | Cells | Levels | What was built |
|---|---|---|---|
s ? a : b | 8 | 1 | 8 $_MUX_ |
c0 ? a : c1 ? b : c2 ? c : d | 24 | 3 | 24 $_MUX_: three muxes in series per bit |
(s == 0) ? a : (s == 1) ? b : (s == 2) ? c : d, 2-bit s | 27 | 4 | 24 $_MUX_ and 3 gates for the compares |
case (s) with four arms, 2-bit s | 60 | 4 | AND and NAND gates: a decode, then an AND-OR per bit |
Measured on this flow. Cell counts compare unlike cells here: a $_MUX_ is larger than a NAND. Levels compare fairly.
The chain grows by one level for each condition. Four choices on one select have the same depth as the case. With sixteen conditions the chain is far deeper than a decode. Use the chain when the conditions really have a priority order.
With an unknown select the ternary gives x where its two inputs differ. Lesson R10.5 uses that to make a hidden unknown show.
Bitwise and Reduction
A bitwise operator takes two vectors and works on each bit position: a & b, a | b, a ^ b, and ~a with one operand. The result is as wide as the operands.
A reduction operator takes one vector and folds all its bits into one: &a, |a, ^a. The symbol is the same. The position tells them apart: a reduction has nothing on its left.
| 8-bit operand | Result width | Cells | Levels |
|---|---|---|---|
a & b | 8 | 8 | 1 |
a ^ b | 8 | 8 | 1 |
~a | 8 | 8 | 1 |
&a: are all bits 1? | 1 | 7 | 3 |
|a: is any bit 1? | 1 | 7 | 3 |
^a: is the number of 1 bits odd? | 1 | 7 | 3 |
Measured on this flow. A reduction of N bits is N - 1 two-input gates.
^a for an 8-bit a. Each stage halves the number of signals.- I/O
- Datapath
- Data
- Pairs
Four gates XOR the bits in pairs.
- Halve again
Two gates combine the four results, then one gate combines those two.
- Count
Seven gates and three levels. Each doubling of the width adds one level.
Read the diagram as text
- a[7:0]: 8 bits
- Stage 1: 4 XOR gates
- Stage 2: 2 XOR gates
- Stage 3: 1 XOR gate
- parity: 1 bit
- a[7:0] → Stage 1: 8 bits
- Stage 1 → Stage 2: 4 bits
- Stage 2 → Stage 3: 2 bits
- Stage 3 → parity: 1 bit
Measured: ^a is 7 cells and 3 levels at 8 bits, 15 and 4 at 16 bits, 31 and 5 at 32 bits.
^a is 1 when a has an odd number of 1 bits. That is a parity bit. Send {^d, d} and the whole word always has an even number of 1 bits.
The Lab: Find the Lost Carry
| Port | Direction | Width | Meaning |
|---|---|---|---|
| a, b | input | [7:0] | the two bytes to add |
| off | input | [3:0] | a signed offset, -8 to 7 |
| word | output | [15:0] | {parity, sum[8:0], ext[5:0]} |
- Latency
- combinational: no clock and no reset
- Corner cases
- a sum above 255, and a negative offset. Done when: Run prints
sum=300on the second line, and Lint is silent
| Bits | Field | Built with |
|---|---|---|
| 15 | parity: the XOR of bits 14 to 0 | a reduction |
| 14:6 | a + b in 9 bits | an add |
| 5:0 | off, sign-extended to 6 bits | a replication and a concatenation |
The word, from the top bit down. The concatenation lists the fields in this order.
Run as shipped
Line 1 is right: 20 plus 22 is 42. Line 2 shows
sum=44for 200 plus 100. Lines 3 and 4 showsum=0.Ask Lint
Lint warns
WIDTHEXPANDon thesumline: the right side "generates 8 bits" where 9 are expected. That is the braces.Fix the width
Write
assign sum = a + b;. Run:sum=300, then 256 twice. The parity bit changes too, because the carry is one of the bits it covers.Check the gates
Synthesize before and after. The shipped design is 45 cells and 16 levels. The fix is 51 cells and 17 levels: the carry needs gates.
Asked in interviews
How do you sign-extend a 4-bit value to 8 bits?
Replicate the sign bit and concatenate:
{{4{d[3]}}, d}. It costs no gates. A size cast on an unsigned value pads with zeros, which is not a sign extension.What is the difference between
a & band&a?a & bis bitwise: one AND per bit position, and a result as wide as the operands.&ais a reduction: it ANDs all bits ofainto one bit.What hardware does a nested ternary build?
A chain of 2-to-1 muxes with priority. The first condition wins. Each condition can add one level, so a long chain is slower than a
casedecode.
Try It
The bench applies four input sets and prints the three fields of word after each. ext is right on every line: a negative offset gets two 1 bits in front.
The sum is wrong whenever it needs the ninth bit. Inside braces a + b is 8 bits wide, so 300 becomes 44 and 256 becomes 0.
Synthesize reports 45 cells and 16 levels, with 0 flops and 0 latches. Most of the depth is the adder's carry chain with the parity tree behind it.
Synthesize the design to measure its cells and inspect the mapped gates.
a=20 b=22 off=3 | parity=1 sum=42 ext=000011 | word=8a83 a=200 b=100 off=7 | parity=0 sum=44 ext=000111 | word=0b07 a=255 b=1 off=-2 | parity=1 sum=0 ext=111110 | word=803e a=128 b=128 off=-8 | parity=1 sum=0 ext=111000 | word=8038Original design · 45 cells · 0 flip-flops · 0 latches · 16 logic levels. Synthesis uses Yosys; counts depend on the toolchain.
Read included file: tb.sv
`timescale 1ns/1ps
module tb;
logic [7:0] a = 0, b = 0;
logic [3:0] off = 0;
logic [15:0] word;
status_word dut (.*);
// Apply one input set, wait for the logic to settle, print the three fields of the word.
task automatic show(input logic [7:0] av, input logic [7:0] bv, input logic [3:0] ov);
a = av; b = bv; off = ov;
#1 $display("a=%0d b=%0d off=%0d | parity=%b sum=%0d ext=%b | word=%h", a, b, $signed(off), word[15], word[14:6], word[5:0], word);
endtask
initial begin
show(8'd20, 8'd22, 4'd3);
show(8'd200, 8'd100, 4'd7); // the sum needs 9 bits
show(8'd255, 8'd1, -4'd2); // and here
show(8'd128, 8'd128, -4'd8); // and here
$finish(0);
end
endmodule
Check Yourself
Common Mistakes
Wrong Write a plain number such as 1 inside braces.
A plain number has no size, so the braces cannot give it a place in the vector.
Fix Give every literal inside braces a size: {1'b1, a}.
assign y = {1, a}; // how wide is 1?assign y = {1'b1, a}; // one bit, then aWrong AND an 8-bit bus with a 4-bit mask and expect the mask to cover both halves.
A bitwise operator first extends the shorter operand with zeros, so the upper half is ANDed with 0.
Fix Make the widths equal yourself. Replicate the mask: data & {2{mask}}.
assign y = data & mask; // mask is 4 bitsassign y = data & {2{mask}}; // 8 bitsWrong Write a long nested ternary where every condition tests the same select.
A nested ternary is a priority chain, and each extra condition can add one more mux in series.
Fix Use a case on the select when the choices exclude each other. Keep the chain for real priority.
Remember
- Braces and replication are wiring: 0 cells. The first operand is the upper bits.
- Each operand inside braces keeps its own width.
{a + b}drops the carry thata + bkeeps in a wider target. - A ternary is one mux per bit. A nested ternary is a priority chain that grows one level per condition.
- Bitwise operators work per bit position and extend the shorter operand with zeros.
- A reduction folds N bits into one with N - 1 gates in a tree.
^ais the parity bit.