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 case by 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.

A Concatenation Is a Set of WiresArrows show where each input bit goes in y = {a, b, {2{a}}}. Nothing is computed.
  • I/O
  • Data
A concatenation is a set of wiresArrows show where each input bit goes in `y = {a, b, {2{a}}}`. Nothing is computed. io a[3:0] 4 bits io b[3:0] 4 bits io y[15:12] a io y[11:8] b io y[7:0] a, then a again io y[15:0] 0 cells, 0 levels wireswirestwo copies
  1. io a[3:0]4 bits
    • wires → y[15:12]
    • two copies → y[7:0]
  2. io b[3:0]4 bits
    • wires → y[11:8]
  3. io y[15:12]a
    • y[15:0]
  4. io y[11:8]b
    • y[15:0]
  5. io y[7:0]a, then a again
    • y[15:0]
  6. io y[15:0]0 cells, 0 levels
  1. Join

    a lands in the top four bits and b in the next four. The order in the braces is the order in the vector.

  2. Repeat

    {2{a}} is a twice, 8 bits. Each wire of a now feeds three places.

  3. 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 targetValueWhy
{4'hA, 2'b11}2bsix bits, placed at the bottom, zeros above
{{4{d[3]}}, d} with d = 4'b1010fafour copies of the sign bit: a sign extension
{4'(d[3]), d} with d = 4'b10101aa size cast pads with zeros: not a sign extension
{4{d[3]}} with d = 4'b10100ffour 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 designCellsLevelsWhat was built
s ? a : b818 $_MUX_
c0 ? a : c1 ? b : c2 ? c : d24324 $_MUX_: three muxes in series per bit
(s == 0) ? a : (s == 1) ? b : (s == 2) ? c : d, 2-bit s27424 $_MUX_ and 3 gates for the compares
case (s) with four arms, 2-bit s604AND 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 operandResult widthCellsLevels
a & b881
a ^ b881
~a881
&a: are all bits 1?173
|a: is any bit 1?173
^a: is the number of 1 bits odd?173

Measured on this flow. A reduction of N bits is N - 1 two-input gates.

A Reduction Is a TreeArrows show bits moving through ^a for an 8-bit a. Each stage halves the number of signals.
  • I/O
  • Datapath
  • Data
A reduction is a treeArrows show bits moving through `^a` for an 8-bit `a`. Each stage halves the number of signals. io a[7:0] 8 bits datapath Stage 1 4 XOR gates datapath Stage 2 2 XOR gates datapath Stage 3 1 XOR gate io parity 1 bit 8 bits4 bits2 bits1 bit
  1. io a[7:0]8 bits
    • 8 bits → Stage 1
  2. datapath Stage 14 XOR gates
    • 4 bits → Stage 2
  3. datapath Stage 22 XOR gates
    • 2 bits → Stage 3
  4. datapath Stage 31 XOR gate
    • 1 bit → parity
  5. io parity1 bit
  1. Pairs

    Four gates XOR the bits in pairs.

  2. Halve again

    Two gates combine the four results, then one gate combines those two.

  3. 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

Design brief
PortDirectionWidthMeaning
a, binput[7:0]the two bytes to add
offinput[3:0]a signed offset, -8 to 7
wordoutput[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=300 on the second line, and Lint is silent
BitsFieldBuilt with
15parity: the XOR of bits 14 to 0a reduction
14:6a + b in 9 bitsan add
5:0off, sign-extended to 6 bitsa replication and a concatenation

The word, from the top bit down. The concatenation lists the fields in this order.

  1. Run as shipped

    Line 1 is right: 20 plus 22 is 42. Line 2 shows sum=44 for 200 plus 100. Lines 3 and 4 show sum=0.

  2. Ask Lint

    Lint warns WIDTHEXPAND on the sum line: the right side "generates 8 bits" where 9 are expected. That is the braces.

  3. 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.

  4. 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
  1. 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.

  2. What is the difference between a & b and &a?

    a & b is bitwise: one AND per bit position, and a result as wide as the operands. &a is a reduction: it ANDs all bits of a into one bit.

  3. 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 case decode.

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.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: 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=8038

Original 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

Predict the outputv is 4'b0110. One concatenation holds a replication, the value and two reductions. What does the bench print?
module tb;
  logic [3:0] v = 4'b0110;
  logic [7:0] y;
  initial begin
    y = {{2{v[3]}}, v, &v, |v};
    $display("y=%h", y);
    $finish(0);
  end
endmodule
Spot the bugEach option extends a signed 4-bit d to 8 bits. The bench tries all sixteen values of d and checks that y holds the same signed number. Which option fails?
ConceptOn this flow, which 8-bit expression builds no cells at all?
ConceptIn y = c0 ? a : c1 ? b : c2 ? c : d, c0 and c2 are 1 and c1 is 0. What is y, and what did this flow build for 8-bit data?
ConceptOn this flow ^a is 7 cells and 3 levels at 8 bits, and 31 cells and 5 levels at 32 bits. What do you expect at 64 bits?

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}.

Before
assign y = {1, a};      // how wide is 1?
After
assign y = {1'b1, a};   // one bit, then a

Wrong 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}}.

Before
assign y = data & mask;        // mask is 4 bits
After
assign y = data & {2{mask}};   // 8 bits

Wrong 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 that a + b keeps 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. ^a is the parity bit.

Practice

Report a problem with this page