Electronic – Hexadecimal two’s Complement

binaryhex

Binary

I have this question, which I thought I knew how to do, however my method is wrong somewhere. I converted the numbers to binary, removed 1 then inverted them to get them in normal binary format. I then added 4 leading 0's to give me a 16 bit number then converted it back to two's complement in hex giving FEF7 Hex, which is the correct answer. However applying the same method to the second number gives F7FE, which is incorrect, as the answer should be 0F7E Hex according to the mark scheme. Can you please tell me where i'm going wrong?

Thanks

Best Answer

Ignacio showed the shortcut: sign-extension.

What you attempted is OK too, and it shows that you try to understand the process: convert to a 'plain value' then convert back to two's complement. But you must remember that this process is different for negative and positive numbers (and 0):

  • for non-negative numbers (highest bit is 0) the value is simply the (positive or 0) value. No need to convert back or forth.

  • for negative numbers (highest bit is 1 ) you can convert back to a positive number, add the extra 0's in front, and then convert back to negative.

You showed that you can do the second, now you only have to remember that this process makes no sense for a value that isn't negative to begin with!