8'hF0 is 1111_0000. As an unsigned value it is 240.
As a two's complement value the top bit has negative weight. The value is −16.
Nothing in the register records which meaning was intended. The declaration and the operators decide.
Operation | Unsigned | Signed | Why |
|---|---|---|---|
Value of | 240 | −16 | MSB weight is +128 vs −128. |
| true | false | −16 is less than 100. |
| 60 | 60 (!) |
|
| 60 | −4 |
|
module tb_repr;
logic [7:0] u = 8'hF0; // unsigned
logic signed [7:0] s = 8'hF0; // same bits, signed
int errors = 0;
initial begin
$display("u=%0d s=%0d", u, s);
if (u !== 8'd240) errors++;
if (s !== -8'sd16) errors++;
if (!(u > 8'd100)) errors++; // unsigned: 240 > 100
if ( s > 8'sd100) errors++; // signed: -16 > 100 is false
if ((u >> 2) !== 8'd60) errors++; // logical shift
if ((s >> 2) !== 8'd60) errors++; // still logical, despite 'signed'
if ((s >>> 2) !== -8'sd4) errors++; // arithmetic shift keeps the sign
if (errors == 0) $display("PASS: all behaviors as described");
$finish;
end
endmoduleFor arithmetic, comparison and bitwise operators, an expression with any unsigned operand is evaluated as unsigned. One unsigned operand strips the sign from everything else.
The shift amount is the exception. The right operand of <<, >>, <<< and >>> does not affect signedness, so a signed value shifted by an unsigned count stays signed (for example s >>> cnt with unsigned cnt still sign-extends).
The sign-extension lesson shows the consequences.
logic [3:0] count; and compared with if (count > -1). Will the branch ever be taken?Never: -1 becomes 4294967295 in the unsigned comparison, so a 4-bit value is never larger than it
Exhaustive check of the compare against -1 on a 4-bit value: the branch is taken for none of the 16 values.