Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if hex value is negative?

Tags:

hex

I have just learned how to read hexadecimal values. Until now, I was only reading them as positive numbers. I heard you could also write negative hex values.

My issue is that I can't tell if a value is negative or positive. I found a few explanations here and there but if I try to verify them by using online hex to decimal converters, they always give me different results.

Sources I found:
https://stackoverflow.com/a/5827491/5016201
https://coderanch.com/t/246080/java-programmer-OCPJP/certification/Negative-Hexadecimal-number

If I understand correctly it means that:
If a hex value written with all its bits having something > 7 as its first hex digit, it is negative.
All 'F' at the beginning or the first digit means is that the value is negative, it is not calculated.

For exemple if the hex value is written in 32 bits:
FFFFF63C => negative ( -2500 ?)
844fc0bb => negative ( -196099909 ?)
F44fc0bb => negative ( -196099909 ?)
FFFFFFFF => negative ( -1 ?)
7FFFFFFF => positive

Am I correct? If not, could you tell me what I am not getting right?

like image 370
aurelienC Avatar asked Nov 10 '15 11:11

aurelienC


People also ask

How do you find a negative number in hexadecimal?

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.


2 Answers

Read up on Two's complement representation: https://en.wikipedia.org/wiki/Two%27s_complement

I think that the easiest way to understand how negative numbers (usually) are treated is to write down a small binary number and then figure out how to do subtraction by one. When you reach 0 and apply that method once again - you'll see that you suddenly get all 1's. And that is how "-1" is (usually) represented: all ones in binary or all f's in hexadecimal. Commonly, if you work with signed numbers, they are represented by the first (most significant) bit being one. That is to say that if you work with a number of bits that is a multiple of four, then a number is negative if the first hexadecimal digit is 8,9,A,B,C,D,E or F.

The method to do negation is:

  1. invert all the bits
  2. add 1

Another benefit from this representation (two's complement) is that you only get one representation for zero, which would not be the case if you marked signed numbers by setting the MSB or just inverting them.

like image 131
Dan Byström Avatar answered Sep 21 '22 18:09

Dan Byström


From what I understand, you always need to look at the left-most digit to tell the sign. If in hex, then anything from 0-7 is positive and 8-f is negative. Alternatively, you can convert from hex to binary, and if there's a 1 in the left-most digit, then the number is negative.

HEX <-> BINARY <-> SIGN
0-7 <-> 0000-0111 <-> pos
8-F <-> 1000-1111 <-> neg

like image 35
Jesse Avatar answered Sep 19 '22 18:09

Jesse