Are hexadecimal numbers ever negative? If yes then how?
For binary you would have signed and unsigned.
How would one represent them in Hex? I need this for a hex routine I am about to embark upon.
-
1Your question is unclear. Are you referring to hexadecimal integer literals? (`0xFFFFFFF == -1` would be an example here). – Paŭlo Ebermann Apr 29 '11 at 01:13
6 Answers
Yes. For example you'd have the following representations in signed 32-bit binary and hex:
Decimal: 1
Binary: 00000000 00000000 00000000 00000001
Hex: 00 00 00 01
Decimal: -1
Binary: 11111111 11111111 11111111 11111111
Hex: FF FF FF FF
Decimal: -2
Binary: 11111111 11111111 11111111 11111110
Hex: FF FF FF FE
As you can see, the Hex representation of negative numbers is directly related to the binary representation.

- 32,176
- 5
- 81
- 116
-
2This follows 2's complement representation of negative numbers as they would be in binary. I know it's the most wide representation of signed binary numbers, however, it's specific to binary, right? This answer takes a base 10, performs 2's compliment, and outputs a base 16 conversion of the literal binary output. I know a "signed" base 10 is represented by a negative sign, is that not the same for base 16? Does -0xF not convert to -15? I know a signed binary requires context, right? because 1111 == -1 or 1111 == 15. The sign adds context. I've never seen a signed hex, and am curious. Thanks! – Kit10 Aug 15 '14 at 11:40
The high bit of a number determines if it is negative. So for instance an int is 32 bits long, so if bit 31 is a 1 it is negative. Now how you display that value be it hexadecimal or decimal doesn't matter. so the hex values like
0x80000000
0x91345232
0xA3432032
0xBFF32042
0xC33252DD
0xE772341F
0xFFFFFFFF
are all negative, because the top bit is set to 1
|
v
0x8 -> 1000
0x9 -> 1001
0xA -> 1010
0xB -> 1011
0xC -> 1100
0xD -> 1101
0xE -> 1110
0xF -> 1111

- 28,272
- 7
- 61
- 66
Yes they can be. It's the same as binary as to how you interpret it (signed vs unsigned).

- 107,317
- 23
- 199
- 210
You would take the binary signed and unsigned forms then represent them in hex as you would any binary number.

- 2,245
- 1
- 20
- 24
On one hand, why not - it's just a positional numeric system, like decimal.
On the other hand, they normally use hex notation to deduce the underlying bit pattern - and that is much more straightforward if the number is interpreted as unsigned.
So the answer is - it's possible, mathematically correct and internally consistent, but defeats the most common purpose of hex notation.

- 59,826
- 25
- 160
- 281
In Java, these are the bounds of the Integer
data type:
Integer.MIN_VALUE = 0x80000000;
Integer.MAX_VALUE = 0x7fffffff;
-
ref code :http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Integer.java#Integer.0MAX_VALUE – roottraveller Aug 03 '17 at 06:38