6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Mon Oct 07, 2024 10:26 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Binary Math
PostPosted: Sun Aug 04, 2013 2:36 pm 
Offline
User avatar

Joined: Wed Jul 10, 2013 3:13 pm
Posts: 67
After learning that I got my checksums terribly wrong :(, I need to ask this question. I am having trouble understanding the 2's compliment. Wikipedia says
Quote:
The two's complement of an N-bit number is defined as the complement with respect to 2N, in other words the result of subtracting the number from 2N. This is also equivalent to taking the ones' complement and then adding one, since the sum of a number and its ones' complement is all 1 bits. The two's complement of a number behaves like the negative of the original number in most arithmetic, and positive and negative numbers can coexist in a natural way.

the N's are in superscript so does that mean 2 to the N power

_________________
JMP $FFD2


Top
 Profile  
Reply with quote  
 Post subject: Re: Binary Math
PostPosted: Sun Aug 04, 2013 2:45 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 9:02 pm
Posts: 1738
Location: Sacramento, CA
In this case, it is 2 to the Nth power (2^N).

For an 8 bit value, 2^N is 256. So, the two's compliment of number x is 256 - x. The one's compliment is 255 - x. That is why they say the two's compliment is the same as the one's compliment plus 1 (256 = 255 + 1).

Daryl

_________________
Please visit my website -> https://sbc.rictor.org/


Top
 Profile  
Reply with quote  
 Post subject: Re: Binary Math
PostPosted: Sun Aug 04, 2013 2:46 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10949
Location: England
To negate a number, you flip all the bits and add one. So, in binary, 00000001 becomes 11111110 and then 11111111. Which is to say, minus 1 is all-ones. This is nicely consistent, because adding 1 to all-ones is obviously going to give you all zeros. And a carry, which you can use or ignore as you see fit.

The neat thing about two's complement is that for addition and subtraction you can ignore it: you go through exactly the same moves for a signed number as you do for an unsigned number. Which is to say, a number isn't signed or unsigned until you treat it as such. Another nice thing is that the top bit is a 1 for a negative number and 0 for a positive one, so the sign of a number is very easy to determine.

Ed


Top
 Profile  
Reply with quote  
 Post subject: Re: Binary Math
PostPosted: Mon Aug 05, 2013 8:46 am 
Offline

Joined: Tue Nov 18, 2003 8:41 pm
Posts: 250
it's the complement in 2 * n
in decimal the 2's complement of 8 is 20 - 8
it's like you've pre subtracted 8 and take a result
via addition mod 20 and interpret 10..19 as negative
numbers
20 - 8 = 12
9 - 8 = 1 = (9 + 12) % 20
5 - 8 = -3 = 5 + 12 = 20 - 3
it's just easy to get the 2's complement
in binary by inverting and adding 1


Top
 Profile  
Reply with quote  
 Post subject: Re: Binary Math
PostPosted: Mon Aug 05, 2013 9:03 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10949
Location: England
?? The appropriate example for decimal is 10's complement, which is the 9's complement plus 1. The 9's complement is just each digit's difference from 9.

For example, the 10's complement of 42 is 57+1, which is 58. To subtract 42 from a number, say 78, you can instead add 58. If you add 58 to 78 you get 36 and a carry. Indeed, 36 is 78-42.

You could also think of 58 as being the difference of 42 from 100, but if you could do multi-digit subtraction you wouldn't need the 10's complement in the first place!

Hope this helps!
Ed


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: