{a, b} places a in the upper bits and b in the lower bits. Every element must have a known width (unsized literals are not allowed inside a concatenation), and the total width is the sum.
{N{x}} repeats x N times. N must be a constant.
Expression | Result (a = 4'b1010, b = 2'b01) | Width |
|---|---|---|
|
| 6 |
|
| 6 |
|
| 4 |
|
| 4 |
|
| 4 |
Read each picture as wires, left to right is most significant to least significant; no gates are involved.
{a, b}: the orange bits of a become the top four bits of the 6-bit result, and b fills the bottom two.
Rotate left: each grey line moves a bit one place up; the orange line carries a[3] round to bit 0, so 1010 becomes 0101.
Four patterns cover most uses: byte order, rotation, sign extension and constant patterns.
A concatenation is unsigned even when it copies a sign bit. {{4{x[3]}},x} constructs the FB encoding for four-bit 1011, but an unsigned sx reads as 251. Assign or cast that encoding into a signed type before a signed comparison or further sign extension; see the casting lesson.
Byte swap reverses the lane order for endianness conversion.
Rotation by one is a concatenation of a slice with the wrapped bit.
Sign extension replicates the sign bit above the value.
Constant patterns repeat a small literal, as in {4{2'b10}}.
h.{h[7:0], h[15:8]}.
The testbench below checks byte order, rotation, sign extension and replication.
module tb_concat;
logic [31:0] word = 32'hDEADBEEF;
logic [31:0] swapped;
logic [7:0] r = 8'b1001_0110;
logic [3:0] x = 4'b1011; // -5 as signed
logic [7:0] sx;
logic signed [7:0] signed_sx;
logic signed [8:0] wide_sx;
int errors = 0;
initial begin
// 1) byte swap (endianness conversion)
swapped = {word[7:0], word[15:8], word[23:16], word[31:24]};
if (swapped !== 32'hEFBEADDE) errors++;
// 2) rotate an 8-bit value left and right by one
if ({r[6:0], r[7]} !== 8'b0010_1101) errors++;
if ({r[0], r[7:1]} !== 8'b0100_1011) errors++;
// 3) sign extension by replicating the sign bit
sx = {{4{x[3]}}, x};
if (sx !== 8'hFB) errors++; // correct sign-extended bits, unsigned value 251
signed_sx = $signed(sx);
wide_sx = signed_sx;
if (signed_sx !== -8'sd5 || wide_sx !== -9'sd5) errors++;
if (!(signed_sx < 8'sd0)) errors++;
// 4) a constant pattern by replication
if ({4{2'b10}} !== 8'b1010_1010) errors++;
if (errors == 0) $display("PASS: concatenation patterns correct");
$finish;
end
endmodule