You are here:
Print

The hexadecimal value of a negative decimal number can be obtained starting from the binary value of that decimal number positive value.
The binary value needs to be negated and then, to add 1. The result (converted to hex) represents the hex value of the respective negative decimal number.

The example below shows how to apply this algorithm, to get the hex value for a 16 bits integer variable that is equal with -10.

Remark: A 16 bits integer variables has values from -32768 to +32767, as is represented below.

Step 1:
The positive value of -10 is 10;

Step 2:
Converted to a 16 bits binary value, 10 will be write as 0000 0000 0000 1010.

Remark: The binary value of 10 (positive decimal number) can be computed as below:

10 divided by 2 = 5 and remains 0
5 divided by 2 = 2 and remains 1
2 divided by 2 = 1 and remains 0
1 divided by 2 = 0 and remains 1

The binary value is obtained by taking the remains from the last one to the first: 1010.
This is a 16 bits variable, so the rest of the bits will be filled with 0 (10 = 0000 0000 0000 1010).

Step 3:
The negate value for 0000 0000 0000 1010 is: 1111 1111 1111 0101

Step 4:
Adding 1 to 1111 1111 1111 0101, will result: 1111 1111 1111 0110.

1111  1111  1111  0101+
0000 0000 0000 0001
——————————-
1111 1111   1111  0110

Step 5:
Converted to hex, the binary value obtained above (1111 1111 1111 0110) represents the hex value of -10 (16 bits integer value): 0xFFF6.

Remark: The conversion to hex of 1111 1111 1111 0110 was done as below.
1111 1111 1111 0110 = 1*2^3+1*2^2+1*2^1+1*2^0 | 1*2^3+1*2^2+1*2^1+1*2^0 | 1*2^3+1*2^2+1*2^1+1*2^0 | 0*2^3+1*2^2+1*2^1+0*2^0 = 15 | 15 | 15 | 6
In hex 10=A, 11=B, 12=C, 13=D, 14=E, 15=F, so the value above (15 | 15 | 15 | 6) will be 0xFFF6.

Previous Fractional numbers reperentation using the 16.16 format