6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Nov 24, 2024 8:45 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Apr 05, 2021 3:58 am 
Offline

Joined: Mon Apr 05, 2021 3:52 am
Posts: 2
Hello, all. This is my first post here, so my apologies if I do anything wrong.

I have been reading Rodnay Zak's "Programming the 6502", and so far it's been helpful. However, one thing I don't think the book explains is when it tells me for an exercise to write a positive integer in "two's complement" (Binary). I understand that if the leftmost bit (bit 7?) is 1, then the number is negative, and that two's complement is the same thing as one's complement but you add 1 (BIN). So my question is, how do I write a positive integer/number in two's complement if inverting the bits turns bit 7 from a 0 to a 1? Again, as much as I like the book, I don't recall it being covered, though I could be wrong.

For example, if I wanted to write 52 in two's complement (00110100), would it be something like 01001011 (not inverting the 7th bit), or would I change the 7th bit as well? (11001011)

Thank you for your time.
-Nick


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 05, 2021 4:13 am 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 704
Location: North Tejas
A binary number with the highest bit clear is considered to be a positive two's complement number.

A single byte represents unsigned numbers in the range 0..255.

There are several ways to represent signed numbers.

One is sign/magnitude in which the upper bit is the sign, 1 for negative. The rest represents the absolute value of the number. The problem with this format is that arithmetic involving negative numbers is awkward. Not to mention there are two representations for zero, positive and negative.

The most common representation supported by computer hardware is two's complement is which a byte represents numbers in the range -128..127. 0 is represented by 0, naturally. 1 by 1 and -1 by $FF. Adding or subtracting two's complement numbers is very easy.

There are other representations with specialized uses.

Maybe this will help to clarify: http://www.mathcs.emory.edu/~cheung/Cou ... ess-n.html


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 05, 2021 4:14 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8546
Location: Southern California
Welcome. I see Bill posted while I was writing. I'll continue anyway.

I was going to explain it, then immediately though, "Wikipedia will probably explain it far better than I, or at least faster;" so here it is:
https://en.wikipedia.org/wiki/Two%27s_complement

If you're old enough to remember the tape counters on tape recorders, think of rewinding past 0, like if you pressed the counter reset button at some place you wanted to reference on the tape. Going backwards past 0, you get to 999 or 9999 which is effectively -1, then 998 or 9998 which is effectively -2, and so on. The digital analogy is that in 8-bit numbers, going down through 0, you get 2, 1, 0, $FF (which is -1), $FE which is -2, etc.. Your 52 (in decimal) is $34 or 34H (ie, in hex), or %00110100 or 00110100B (in binary). The fact that bit 7 is 0 means it's positive. If you wanted -52, or -34H, that would be 11001100B, which, in unsigned numbers, is 204 or $CC. If you're dealing with 16-bit numbers, the sign is in bit 15 instead of bit 7.

Like Bill said, it works out nicely for adding and subtracting. The processor doesn't really care if you're considering the numbers signed or not. The answer is the same. The difference is in how you interpret it.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 05, 2021 5:26 am 
Offline

Joined: Mon Apr 05, 2021 3:52 am
Posts: 2
Thank you for the responses!

So the 7th bit would just be 0, still. Thanks!


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 05, 2021 7:46 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
Welcome! Yes indeed: bits only get flipped when dealing with a negative number.

In fact two's complement wraps around zero in a nicely regular way: there's no discontinuity as you count up from negative numbers into positive numbers:
1111 1110
1111 1111
0000 0000
0000 0001
That is, the usual rules for adding one and propagating carries.


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 3 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: