@rkenmi - 2's complement on long hexadecimals

2's complement on long hexadecimals


Binary Finary

2's complement on long hexadecimals


Back to Top

Updated on February 5, 2017

2's complement is originally done on binary numbers, but sometimes you might need a binary 2's complement representation of hexadecimal digits. If a hexadecimal value is short enough (say 4 characters), there are a number of methods you can use to get the 2's complement.

For starters, you can convert a hexadecimal to binary and do a 2's complement, then convert back to hexadecimal for a straightforward answer.

Suppose you have the following hexadecimals: 0x5DA0
Binary representation: 0101 1101 1010 0000

2's complement of binary numbers:

  • Inverse the binary number (called 1's complement)

    • 1010 0010 0101 1111
  • Add 1 to LSB (least significant bit)

    • 1010 0010 0101 1111 + 1 = 1010 0010 0110 0000

Convert back to hexadecimal:

  • Convert each binary block to decimal.

    • 1010 0010 0110 0000 = 10, 2, 6, 0
  • Convert each decimal block to hexadecimal.

    • 10, 2, 6, 0 = A, 2, 6, 0 = A260 = 0xA260

But for very long hexadecimals, this is going to be a pain. Imagine if you had 16 hexadecimal characters. Each hexadecimal characters will have 4 bits, so you are going to be dealing with 64 bit binary numbers. Not fun.

Thankfully, there is an easy way to get the 2's complement of long hexadecimals.

Suppose now that your hexadecimal characters are: A034 5D24 FF20 A120

2's complement of (long) hexadecimals:

  • Subtract it from a hexadecimal value of 10 ^ (n + 1), where n = number of characters of original hexadecimals.
    • In this case, A034 5D24 FF20 A120 is 16 characters. So the hexadecimal number to subtract from is 10 ^ (16 + 1) = 10 ^ 17 = 1 0000 0000 0000 0000. It looks like binary but don't be fooled.
    • 1 0000 0000 0000 0000 - A034 5D24 FF20 A120

This will give you an answer of:

alt

And there you have it, 2's complement of a lengthy hexadecimal address. The subtraction isn't too bad either, since most of the characters are subtracted from a hexadecimal 'F'.


Article Tags:
binaryhexadecimal2's Complement