Browse
 
Tools
Rss Categories

Negative numbers representation in hex

Author: AM Reference Number: AA-15440 Views: 79386 Created: 21-05-2018 12:54 Last Updated: 28-04-2021 17:57 0 Rating/ Voters

The hexadecimal value of a negative decimal number can be obtained starting from the binary value of that decimal number's absolute value.
The binary value needs to be logically inverted and then 1 needs to be added to that. 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 absolute value of -10 is 10;

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

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

10 divided by 2 = 5, remainder 0
5 divided by 2 = 2, remainder 1
2 divided by 2 = 1, remainder 0
1 divided by 2 = 0, remainder 1

The binary value is obtained by taking the remainders 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 d = 0000 0000 0000 1010 b).

Step 3:
The logical inversion 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.


Rss Comments
  • There are no comments for this article.