Bitwise operators (AND, OR, XOR, NOT) in Verilog - Time & Space Complexity
We want to understand how the time to run bitwise operations changes as the input size grows.
How does the number of bits affect the work done by AND, OR, XOR, and NOT?
Analyze the time complexity of the following code snippet.
module bitwise_ops #(
parameter int N = 32
) (
input logic [N-1:0] a, b,
output logic [N-1:0] and_out,
output logic [N-1:0] or_out,
output logic [N-1:0] xor_out,
output logic [N-1:0] not_out
);
assign and_out = a & b;
assign or_out = a | b;
assign xor_out = a ^ b;
assign not_out = ~a;
endmodule
This module performs bitwise AND, OR, XOR, and NOT on two N-bit inputs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Bitwise operation on each bit of the input vectors.
- How many times: Once per bit, so N times for inputs of size N.
Each bit is processed independently, so the total work grows directly with the number of bits.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 bitwise operations per operator |
| 100 | 100 bitwise operations per operator |
| 1000 | 1000 bitwise operations per operator |
Pattern observation: The operations increase linearly as input size increases.
Time Complexity: O(n)
This means the time to complete the bitwise operations grows in direct proportion to the number of bits.
[X] Wrong: "Bitwise operations take constant time regardless of input size."
[OK] Correct: Each bit must be processed, so more bits mean more work, making time grow with input size.
Understanding how bitwise operations scale helps you reason about hardware design and low-level code efficiency, a useful skill in many technical discussions.
"What if we used a fixed-size 32-bit input instead of variable size N? How would the time complexity change?"
Practice
~ is the symbol for which operator?Solution
Step 1: Understand the operator symbol
The symbol~in Verilog is used to flip bits, meaning it changes 0 to 1 and 1 to 0.Step 2: Match symbol to operator name
This flipping action corresponds to the NOT operator, which inverts each bit.Final Answer:
NOT -> Option BQuick Check:
~ = NOT [OK]
- Confusing ~ with AND (&)
- Thinking ~ is OR (|)
- Mixing ~ with XOR (^)
a and b?Solution
Step 1: Identify bitwise AND operator
In Verilog, the bitwise AND operator is&, which works bit by bit on vectors.Step 2: Check syntax correctness
The syntaxassign c = a & b;correctly assigns the bitwise AND ofaandbtoc.Final Answer:
assign c = a & b; -> Option AQuick Check:
& is bitwise AND [OK]
- Using && which is logical AND
- Confusing & with | (OR)
- Using ^ which is XOR
assign result = 4'b1101 ^ 4'b1011;Solution
Step 1: Understand XOR operation
XOR (^) compares bits: if bits differ, result is 1; if same, result is 0.Step 2: Calculate bitwise XOR
Bits: 1101 ^ 1011
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0
Result = 0110Final Answer:
4'b0110 -> Option CQuick Check:
1101 ^ 1011 = 0110 [OK]
- Mixing XOR with OR or AND results
- Calculating bits incorrectly
- Confusing bit order
assign out = a |& b;
where
a and b are 4-bit vectors.Solution
Step 1: Check operator validity
The operator|&is not a valid bitwise operator in Verilog; it is a reduction operator but used incorrectly here.Step 2: Understand correct bitwise OR usage
For bitwise OR between vectors, use|alone, likea | b.Final Answer:
Operator|&is invalid for bitwise operations -> Option DQuick Check:
|& is invalid here [OK]
- Confusing reduction and bitwise operators
- Forgetting semicolon (though present here)
- Using logical OR || instead of bitwise |
a = 4'b1010 and b = 4'b1100, which Verilog expression produces a 4-bit vector where bits are set to 1 only if the corresponding bits in a and b differ?Solution
Step 1: Understand the requirement
We want bits set to 1 only whereaandbdiffer.Step 2: Identify operator for differing bits
The XOR operator (^) sets bits to 1 where bits differ and 0 where they are the same.Step 3: Apply XOR to given vectors
Fora = 1010andb = 1100,a ^ b = 0110, which matches the requirement.Final Answer:
assign c = a ^ b; -> Option AQuick Check:
XOR gives 1 for differing bits [OK]
- Using AND (&) which gives common bits
- Using OR (|) which sets bits if either is 1
- Using NOT (~) which flips all bits
Ready to prove it?
Pass the test and this pattern moves unknown
