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.
- DEC to BIN without the sign:
- 23 → 0001 0111
- Negate the BIN string:
- 0001 0111 → 1110 1000
- Add 1 to the negated BIN result:
- 1110 1000 + 0000 0001 → 1110 1001
- Verify the correct two's complement calculation:
- -128 + 64 + 32 + 8 + 1 = -23 → correct
- Convert final BIN string to HEX:
- 1110 1001 → 0xE9