Are hexadecimal numbers ever negative?
Are hexadecimal numbers ever negative? If yes then how?
For binary you wou开发者_开发问答ld have signed and unsigned. How would one represent them in Hex? I need this for a hex routine I am about to embark upon.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.
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
Yes they can be. It's the same as binary as to how you interpret it (signed vs unsigned).
You would take the binary signed and unsigned forms then represent them in hex as you would any binary number.
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.
In Java, these are the bounds of the Integer
data type:
Integer.MIN_VALUE = 0x80000000;
Integer.MAX_VALUE = 0x7fffffff;
精彩评论