Consider the following piece of Verilog code: reg [3:0] u_a; reg signed […
Consider the following piece of Verilog code: reg [3:0] u_a; reg signed [3:0] s_b; reg signed [7:0] out1, out2, out3; initial begin u_a = 4'b1100; // 12 in unsigned s_b = -4'sd4; // -4 in 4-bit 2's complement (4'b1100) out1 = s_b >>> 2; out2 = u_a + s_b; out3 = $signed(u_a) + s_b; end Which of the following statements regarding out1 , out2 , and out3 are **TRUE**? *(Select all that apply)*
- out1 evaluates to 8'sb11111111 (-1 in decimal) because sign extension fills arithmetic right shifts on signed types.
- out2 evaluates to 8'd8 because s_b preserves its signed property during addition with u_a .
- out2 evaluates to 8'd24 because the presence of an unsigned operand ( u_a ) coerces the entire expression (u_a + s_b) into an unsigned context.
- out3 evaluates to 8'sh0008 (decimal +8).
- out1 evaluates to 8'sb00000011 because arithmetic right shifts treat the vector as unsigned unless wrapped in $signed() .
Correct answer
A. out1 evaluates to 8'sb11111111 (-1 in decimal) because sign extension fills arithmetic right shifts on signed types.; C. out2 evaluates to 8'd24 because the presence of an unsigned operand ( u_a ) coerces the entire expression (u_a + s_b) into an unsigned context.; D. out3 evaluates to 8'sh0008 (decimal +8).
Explanation
* **A is TRUE & E is FALSE:** s_b is explicitly declared as reg signed [3:0] . In Verilog-2001, applying >>> to a variable declared as signed performs a true arithmetic right shift with sign extension: 4'b1100 >>> 2 yields 4'b1111 (decimal -1). When assigned to 8-bit signed out1 , sign extension yields 8'b11111111 . * **B is FALSE & C is TRUE:** In Verilog, if **any** operand in an operation is unsigned, the entire expression is coerced into an **unsigned** evaluation context. s_b ( 4'b1100 ) is treated as unsigned integer 12. Therefore, u_a + s_b = 12 + 12 = 24 ( 8'b00011000 ). * **D is TRUE:** Using $signed(u_a) casts u_a ( 4'b1100 = -4) into signed format. Now both operands are signed: $(-4) + (-4) = -8$? Wait: Let us compute $signed(u_a) : bit pattern is 4'b1100 . In two's complement 4-bit, 4'b1100 represents $-4$. Wait! Let's check $12$ vs $-4$: If u_a = 4'b1100 , $signed(u_a) has value $-4$. s_b = -4'sd4 = 4'b1100 , which is $-4$. $(-4) + (-4) = -8 = 8'b11111000$. Wait, what does option D say? "out3 evaluates to 8'sh0008 (decimal +8)". That would mean D is FALSE! Let us re-verify: $u\_a = 4'b1100$. As signed 4-bit, MSB is 1, so value is $-(16 - 12) = -4$. $s\_b = -4'sd4 = 4'b1100 = -4$. $-4 + -4 = -8$. In 8-bit two's complement, $-8$ is 8'hF8 , NOT 8'h08 . Therefore, **D is FALSE**. Let us review the correct choices: Only **A and C are TRUE**. **Correct Answer:** A and C **Explanation:** * **A is TRUE:** s_b is declared signed . Shift arithmetic 4'b1100 >>> 2 yields 4'b1111 (-1). Extending to 8-bit out1 yields 8'b11111111 ( -1 ). * **B is FALSE, C is TRUE:** According to IEEE 1364-2001, if any operand of an operation is unsigned, the entire expression becomes unsigned. s_b is re-interpreted as unsigned 4'd12 . Thus $12 + 12 = 24$. * **D is FALSE:** $signed(4'b1100) is interpreted as $-4$. Thus $(-4) + (-4) = -8$, which extends to 8'b11111000 ( 8'hF8 ), not +8 . * **E is FALSE:** s_b is already declared signed; it does not need $signed() .