# How to tell if hex value is negative?

## How to tell if hex value is negative?

Read up on Twos 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 – youll see that you suddenly get all 1s. And that is how -1 is (usually) represented: all ones in binary or all fs 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

Another benefit from this representation (twos 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.

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 theres 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

#### How to tell if hex value is negative?

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 its 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 its 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.