Question

I'm wondering if I get a question like:

"Convert a decimal number to two's complement, then give your answer in Hex".

Is the path below, how one would do it?

Decimal number: -23

23 = 00010111 = in hex 17 = -17

-23 = 11101001 = in hex E9

So to convert it to Hex would the answer be -17 or E9?

Thanks

Was it helpful?

Solution

The -17 has no relevance here, since according to your task, you have to return the two's complement as HEX and that is E9.

Your conversion path in general looks correct to me.

  1. DEC to BIN without the sign:
    • 23 → 0001 0111
  2. Negate the BIN string:
    • 0001 0111 → 1110 1000
  3. Add 1 to the negated BIN result:
    • 1110 1000 + 0000 0001 → 1110 1001
  4. Verify the correct two's complement calculation:
    • -128 + 64 + 32 + 8 + 1 = -23 → correct
  5. Convert final BIN string to HEX:
    • 1110 1001 → 0xE9
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top