21

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?

Joshua
  • 177
  • 3
  • 3
  • 14
aurelienC
  • 1,113
  • 3
  • 12
  • 22
  • 13
    Strictly speaking you can't tell just by looking at the number whether it's negative or positive; you also need to know how wide the number is supposed to be, and whether it's supposed to be signed or unsigned. – Ignacio Vazquez-Abrams Nov 10 '15 at 12:19
  • @IgnacioVazquez-Abrams Okay, let's say i know the number is signed 32bits, is what i assert (in bold) in my question true? – aurelienC Nov 10 '15 at 13:25
  • 1
    It's simplified *just* enough to be inaccurate. You need to look at bit n-1 where n is the bitsize and the rightmost bit is bit 0. – Ignacio Vazquez-Abrams Nov 10 '15 at 13:28
  • @IgnacioVazquez-Abrams Thanks, i see more clearly now. – aurelienC Nov 10 '15 at 13:41

4 Answers4

21

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.

Dan Byström
  • 9,067
  • 5
  • 38
  • 68
  • 2
    “While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.” – Kromster Nov 10 '15 at 12:01
18

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

Jesse
  • 439
  • 4
  • 6
  • 1
    but then why hex `%p` = `0x7ffc9f7662f0`, and the same printed as `%d` = `-1619631376`, hex started with `7`, yet is negative – Herdsman Jan 11 '20 at 12:00
  • 1
    @Herdsman: Because `%d` only prints an `int` (32-bit in your case), so it only looked at the low 32 bits of that 64-bit arg you passed. `0x9f7662f0` is a negative int32_t. The compiler should have warned you about the format mismatch. `%p` is intended for pointers; use `%#llx` to print unsigned long long hex or `%lld` to print `long long`. https://en.cppreference.com/w/cpp/io/c/fprintf – Peter Cordes Oct 08 '21 at 02:58
  • This answer works as long as there aren't any leading zeros omitted. e.g. a 16-bit number holding `0xFF` needs to be written as `0x00FF` to apply this rule. – Peter Cordes Oct 08 '21 at 03:01
4

The answer here in the forum looks good:

Each hexadecimal "digit" is 4 bits. The d in the high order position is 1101. So you see it's got a high bit of one, therefore the whole number is negative.

and

A hex number is always positive (unless you specifically put a minus sign in front of it). It might be interpreted as a negative number once you store it in a particular data type. Only then does the most significant bit (MSB) matter, but it's the MSB of the number "as stored in that data type". In that respect the answers above are only partially correct: only in the context of an actual data type (like an int or a long) does the MSB matter.

If you store "0xdcafe" in an int, the representation of it would be "0000 0000 0000 1101 1100 1010 1111 1110" - the MSB is 0. Whereas the representation of "0xdeadcafe" is "1101 1110 1010 1101 1100 1010 1111 1110" - the MSB is 1.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

You can tell whether a hexadecimal integer is positive or negative by inspecting its most significant (highest) digit. If the digit is ≥ 8, the number is negative; if the digit is ≤ 7, the number is positive. For example, hexadecimal 8A20 is negative and 7FD9 is positive

Shams Ansari
  • 762
  • 5
  • 12