Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal <--> Two's Complement <--> Hex conversion

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

like image 294
DangerousDave23 Avatar asked Oct 26 '25 15:10

DangerousDave23


1 Answers

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
like image 105
Philip Allgaier Avatar answered Oct 29 '25 07:10

Philip Allgaier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!