I need to convert the following hexadecimal representation of 2’s complement binary numbers to decimal numbers I am unsure if I am doing it correctly or am I missing a step?
a. xF0
b. x7FF
c. x16
d. x8000
My Attempt at first converting it to a Decimal then Binary:
a. xF0 = 240 = 11110000
b. x7FF = 2047 = 11111111111
c. x16 = 22 = 10110
d. x8000 = 32768 = 1000000000000000
You're doing it correctly, but you're missing a step. You know how to obtain the unsigned value, but you're missing the next step that gives you the signed value.
Let's call $c$ the unsigned value, $\mathcal{L}$ the bit length and $n$ the signed value. Like Tony, I'm going to assume that a. and c. are supposed to be 8-bit, and b. and d. are supposed to be 16-bit. This means then that $\mathcal{L} = 8$ or 16. To get $n$ from $c$, you
XOR$c$ with $2^\mathcal{L} - 1$, add 1 and then multiply by $-1$. Or much easier, $n = c - 2^\mathcal{L}$.a.
xF0as an unsigned byte would indeed mean 240. But what does it mean as a signed byte? It's a very well chosen example for educational purposes, since, as Dave explains, each hexadecimal digit corresponds to four bits, and sincexFis four bits on andx0is four bits off, they "toggle" to each other (which is the same as what Tony calledXORing withxFF), that is,xF0toggles tox0F, and that means 15 in decimal. Now you addx01to obtainx10, which is 16. SoxF0as a signed byte means $-16$. You can doublecheck by seeing that $240 - 256 = -16$.b. If this is supposed to be a 16-bit word, e.g.,
x07FF, the sign bit is 0, so you're done. It's 2047, just like you said.c. Whether this is a byte or a word, the sign bit is also 0 and you're done.
d. This one can be very confusing if you're the slightest bit dyslexic, so you have to take it step-by-step. Do
x8000 XOR xFFFFto obtainx7FFF, which means 32767. Add 1 to obtain 32768 and multiply by $-1$ to obtain $-32768$. Verify this with the subtraction method: we have $c = 32768$ and $\mathcal{L} = 16$, so then $2^\mathcal{L} = 65536$, and therefore $n = 32768 - 65536 = -32768$.Did d. feel circuitous and confusing, and make you question why we use two's complement at all and not some simpler method (such as just a sign bit without two's complement)? The answer is that we don't use two's complement, computers do. And for computers, two's complement solves two important problems: it ensures a unique representation for 0 and preserves the least significant bit as the parity bit. But I might be stepping on your teacher's toes with that, so I won't say anything more about it.
Lastly, I promised I would mention Programmer Mode in the Windows Calculator. I'm using Windows 8. In the Desktop app, I press Windows-R and type
calcto invoke the Windows 8.0 Calculator 6.2. Then I switch from Scientific Mode to Programmer Mode by pressing Alt-3. (Or you can use the menu: View -> Programmer). You can set it to byte, word, double word ($\mathcal{L} = 32$) or quadruple word ($\mathcal{L} = 64$). Notice also the Xor button. To work through exercises a. through d. with the Xor button, use either the Dword or Qword setting. And then just to be extra sure, set it to Word and compute $-2 * 2$. Then repeatedly press Enter until you reach $-32768$. Take a look just a little bit below where it says $-32768$. Look familiar? And lastly, set it to Byte and compute $0 - 16$.By the way, the Mac OS X Calculator also has Programmer Mode.