Are hexadecimal numbers ever negative?

后端 未结 6 913
感情败类
感情败类 2021-02-14 09:39

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

相关标签:
6条回答
  • 2021-02-14 10:07

    In Java, these are the bounds of the Integer data type:

    Integer.MIN_VALUE = 0x80000000; 
    Integer.MAX_VALUE = 0x7fffffff;
    
    0 讨论(0)
  • 2021-02-14 10:09

    Yes they can be. It's the same as binary as to how you interpret it (signed vs unsigned).

    0 讨论(0)
  • 2021-02-14 10:13

    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
    
    0 讨论(0)
  • 2021-02-14 10:13

    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.

    0 讨论(0)
  • 2021-02-14 10:18

    You would take the binary signed and unsigned forms then represent them in hex as you would any binary number.

    0 讨论(0)
  • 2021-02-14 10:22

    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.

    0 讨论(0)
提交回复
热议问题