# discrimination between normal and negative number stored in 2's compliment form

Status
Not open for further replies.

##### Member level 1
Hi,
While defining test case for a hardware, i had to use following in statement:
-8'd3;
Now this statement means -3 in decimal or 8-bit binary stored as 2's complement of binary 3, i.e 8'b1111_1101. Now when we write 8'b1111_1101, it can be -3 or (2^8)-3=253 in decimal. So how does compiler decide whether it's -3 or 253?

The compiler decision is based on the declaration of variable made in the startup. so if the variable x is declared as
unsigned int x; //then value is 253 or if
singned int x; // otherwise value is -3

The compiler decision is based on the declaration of variable made in the startup. so if the variable x is declared as
unsigned int x; //then value is 253 or if
singned int x; // otherwise value is -3
Hi, that's correct in HLLs. In verilog suppose i define x as
reg [7:0] x;
x=-8'd3;
In this case, how compiler will decide if it's 253 or -3.

Compiler does not care for negative number in any operation.
Designer has to take care about it. For example

a=8=4'b1000
b=-3=4'b1101 (2's complement)

if you are doing s=a+b
s=8+(-3)= 4'b1000 +4'b1101 = 5'b10101 . Discard the last 1. Because you that it was negative number.

if b was not assigned by, its output of another operation. then you should check whether it is negative number or positive number.

Status
Not open for further replies.