How do you convert a negative number to hex.
e.g. -127 converts to 81H
What is the proceedure to work this out without using a calculator?
How do you convert a negative number to hex.
e.g. -127 converts to 81H
What is the proceedure to work this out without using a calculator?
First, convert the absolute value to a hexadecimal number.
Second, invert the bits.
Third, add 1.
127 = 0x7f
~0x7f = 0x80
0x80 + 0x1 = 0x81
How do you know that 0x81 isn’t being used to represent 129?
Hex represents raw data. Computers can interpret that in several ways. Before you can make sense of it you need to know what representation is being used.
BTW the method described by Punoqllads is known as ‘Twos compliment’ and is used by most computers, certainly all PCs. ‘Ones compliment’ avoids the ‘add one’ step, is easier to convert in your head, but does have the annoying property of allowing both positive and negative zero!
Thanks very much,
in case you are wondering i am trying to trace through a small mc6800 program.
an was curious why i was getting different test data this would explain a few things
again
cheers
I’ve thought about it, and I’m stuck. I gotta know. Why are you tracing through a hex program without knowing how to represent hex numbers? Are you supremely overconfident, a lawyer perhaps?
i am studying assembly code at uni, and its been about 6 years since i had to do this without a calculator and finding any resources about 1’s and 2’s compliment to hex with the negative flags etc. has been difficult to find, but i’m ok now.
and no i am not a lawyer, i refine petrol , much worse…