HDL & Verification
wire [7:0] a = 8'hFF;
wire gt = (a > 8'd1);Intending a to be signed -1, someone wrote the code above, and gt became 1. Why?
- AWithout a signed declaration, a was compared as unsigned 255
- BComparisons in Verilog always return 1
- C8'hFF equals 0
Show answer and explanation
Answer: A. Without a signed declaration, a was compared as unsigned 255
Verilog treats signals as unsigned unless declared signed, so 8'hFF is 255, and 255 > 1 is true. Declaring a as "wire signed [7:0]" and also making the constant signed, as in 8'sd1, compares a as two's-complement -1, giving 0. Sized or based constants such as 8'd1 are unsigned, and if any operand in an expression is unsigned the whole expression is evaluated unsigned, so declaring only a as signed still leaves gt at 1. Signed arithmetic therefore requires keeping all operand types consistent.
- A. Correct. Signed treatment requires a signed declaration.
- B. Comparisons yield 0 or 1 depending on the values.
- C. 8'hFF is 255 (or -1 if signed).