Title: How to tell if hex value is negative?
Tags: hex
Question: 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?

Comment: 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.

Comment: 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.

Comment: @IgnacioVazquez-Abrams Okay, let's say i know the number is signed 32bits, is what i assert (in bold) in my question true?

Comment: @IgnacioVazquez-Abrams Thanks, i see more clearly now.

Here is the accepted answer: 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:


invert all the bits
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.

Comment for this answer: “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.”

Here is another answer: 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


Here is another answer: 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.
```


Here is another answer: 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

Comment for this answer: @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
Comment for this answer: 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.
Comment for this answer: but then why hex `%p` = `0x7ffc9f7662f0`, and the same printed as `%d` = `-1619631376`, hex started with `7`, yet is negative

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2015:00:11/10/15', 'FavoriteCount': 6.0, 'PostTypeId': 1, 'Tags': '<hex>', 'Title': 'How to tell if hex value is negative?', 'ViewCount': 128549, 'id': 33629416, 'user_AboutMe': "<p>J'adore l'eau, dans 20-30 ans il y en aura plus.</p>\n", 'user_AccountId': 6478259.0, 'user_DisplayName': 'aurelienC', 'user_Reputation': 1033.0, 'user_Views': 112.0, 'user_id': 5016201.0}}