Concatenation and replication in Verilog - Time & Space Complexity
We want to see how the time to run Verilog code changes when using concatenation and replication.
How does the number of operations grow as we increase the size of the data being combined?
Analyze the time complexity of the following code snippet.
module concat_replication(input [3:0] a, input [3:0] b, output reg [15:0] out);
always @(*) begin
out = {4{a}}; // replicate 'a' 4 times
out = {a, b, a, b}; // concatenate 'a' and 'b' twice
end
endmodule
This code replicates a 4-bit input 4 times and concatenates two 4-bit inputs multiple times to form a 16-bit output.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Replication repeats the input bits multiple times; concatenation joins multiple inputs side by side.
- How many times: Replication repeats 4 times; concatenation combines 4 parts once.
As the input size grows, the number of bits to replicate or concatenate grows too.
| Input Size (bits) | Approx. Operations |
|---|---|
| 4 | 4 (replications) x 4 bits = 16 bit operations |
| 8 | 4 x 8 = 32 bit operations |
| 16 | 4 x 16 = 64 bit operations |
Pattern observation: The operations grow linearly with the input size multiplied by the replication count.
Time Complexity: O(n)
This means the time to replicate or concatenate grows directly in proportion to the input size.
[X] Wrong: "Concatenation or replication happens instantly, no matter the input size."
[OK] Correct: Each bit must be copied or joined, so larger inputs take more steps, not zero time.
Understanding how data size affects operations like concatenation helps you reason about hardware design efficiency and timing.
"What if we increased the replication count from 4 to n? How would the time complexity change?"
Practice
{4{1'b0}} produce?Solution
Step 1: Understand replication syntax
The expression{4{1'b0}}means repeat the bit 0 four times.Step 2: Construct the resulting vector
Repeating 1'b0 four times creates a 4-bit vector: 0000.Final Answer:
A 4-bit vector with all bits set to 0 -> Option AQuick Check:
Replication repeats bits = 0000 [OK]
- Confusing replication with concatenation
- Thinking it produces a single bit with value equal to the number
- Assuming it creates all ones instead of zeros
a and a 2-bit vector b in Verilog?Solution
Step 1: Recall concatenation syntax
Concatenation in Verilog uses curly braces with commas between signals.Step 2: Identify correct syntax
The correct way to join vectorsaandbside by side is{a, b}.Final Answer:
{a, b} -> Option CQuick Check:
Concatenation uses braces and commas = {a, b} [OK]
- Using parentheses instead of braces
- Using plus sign inside braces incorrectly
- Using square brackets which are invalid here
{2{2'b10}, 4'b0011}Solution
Step 1: Evaluate the replication part
2'b10is 2 bits: 10. Replicated twice: 10 10.Step 2: Concatenate with 4'b0011
Concatenate 10 10 (4 bits) with 0011 (4 bits) to get 10100011.Final Answer:
10101011 -> Option AQuick Check:
Replication + concatenation = 10101011 [OK]
- Miscounting bits after replication
- Concatenating in wrong order
- Confusing binary digits during replication
wire [7:0] out;
assign out = {3{2'b11}, 3'b101};Solution
Step 1: Calculate bits from replication
3{2'b11}replicates 2 bits three times = 6 bits total.Step 2: Calculate bits from concatenation
Concatenating 6 bits with 3 bits (3'b101) gives 9 bits total, butoutis 8 bits wide.Step 3: Identify size mismatch error
The assigned value is 9 bits, butoutexpects 8 bits, causing a size mismatch error.Final Answer:
Concatenation operands have mismatched bit widths causing size mismatch -> Option BQuick Check:
Bit width mismatch in concatenation = error [OK]
- Ignoring bit width mismatch warnings
- Assuming replication count must be variable
- Thinking replication syntax needs parentheses
sig by repeating a 3-bit pattern 3'b101 four times using the replication operator. Which Verilog expression correctly does this?Solution
Step 1: Understand replication for repeating patterns
Using{4{3'b101}}repeats the 3-bit pattern four times, producing 12 bits.Step 2: Compare with other options
Manual concatenationassign sig = {3'b101, 3'b101, 3'b101, 3'b101};repeats explicitly but doesn't use the replication operator;assign sig = {3'b101 * 4};is invalid syntax;assign sig = {4(3'b101)};is incorrect syntax.Final Answer:
assign sig = {4{3'b101}}; -> Option DQuick Check:
Replication repeats pattern cleanly =assign sig = {4{3'b101}};[OK]
- Using multiplication instead of replication
- Writing replication without braces
- Manually concatenating repeated patterns instead of replicating
Ready to prove it?
Pass the test and this pattern moves unknown
