Search code examples
hexsubtractiontwos-complement

Two's Complement Hex Subtraction


I'm looking at

0x0 - 0x21524111

in a two's comp system. I know the answer (DEADBEEF) but I'm uncertain about how to figure out these kinds of subtractions efficiently. Since I can't do carry-overs with 0x0, what should I do to get the result?


Solution

  • Any subtraction in a two's complement system is really just an addition of the compliment. What you would need to do is take the inverse of 0x21524111 which becomes 0xDEADBEEF, then add it to 0x0 which is still 0xDEADBEEF.

    Edit: Adding in that the 'inverse' means two's complement which means -a really is ~a + 1