Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble understanding signed numbers

Status
Not open for further replies.

stillLearning666

Programmer
Sep 3, 2002
1
CA
I've been studying hard "the art of assembly language", and I can't quite grasp the concept of signed numbers. Can someone point me to a good document or manual that I can use to figure this out? I need another angle.
For example, the eight bit two's comlement value for "-64" is supposed to be "0C0"hex. And the 16bit equivalent would be "0FFC0"hex. I can't figure out how "-64" equates those results. I've got no problem figuring out the positive value of "64", but I'm stumped with the negative part. Any help would be greatly appreciated.
thanks,
stillLearning666
 
Here's a *simple* method of getting the negative representation of the number:
Put the number in BINARY.
Set all 0's to 1's and 1's to 0's.
Add 1.
Put the number back into HEX.


Whoever created the two's complement form of negative numbers was a real genius. This is because -1 would equate to 0FFh in 8-bit. Add this number to 1, and you get 100h. However we are in 8-bit so 1 gets lopped off, and we get 00h. +1 -1 =0. This works EVERY TIME WITH ALL NEGATIVE NUMBERS. Add 0FFh to 2, you get 101h, lop off the starting 1 (since we are 8-bit...) you get 01h. +2 -1 =1. Now isn't that NICE or WHAT?

"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
To get two's complement of a number take the bit representation of it's positive counterpart (64 in this case), invert every bit, that is 0 becomes 1 and 1 becomes 0, then add 1 and you have got it's two's complement.

E.g. if we have 00011100 (28) we invert it. The result will be 11100011, then we add one, getting 11100100. This is the two's complement meaning -28.

The reason for adding one is that if we would skip this both +0 and -0 would be representable, this is not that nice (this is known as one's complement, simply invert the positive number to get its negative counterpart). Also when doing arithmetics with one's complement numbers we cannot use the normal addition and so on that we can use for unsigned numbers.

Two's complement however, has some very nice features; adding -28 and 28 gives the result 0 with normal unsigned addition. This works for all numbers, thus adding -3, -6 and 19 will give the result 10 with normal binary addition, as used for unsigned numbers. (try it yourself to see that).

This adding of one is also the reason why you get assymetrical max and min for signed numbers, e.g. with one byte you can represent -128 to 127 (since the zero is in the positive space).

If you want more info check out this page (the first hit when I googled for "two's complement" ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top