binary numbers: 2's complement

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

User avatar
jargon
B&: This is not your blog
Posts: 208
Joined: Fri Dec 07, 2007 11:40 pm
Location: 480/85260
Contact:

Post by jargon »

yeah i was trying to show how you did it versus what Disch ended up explaining.

Code: Select all

f(x){
return (x&0x80)?-((x&0x80)-(x&0x7f)):x;
} 
simplifies as -128+(foo&0x7f) for negative numbers only. (like Disch said)


blargg made this even simpler:

Code: Select all

f(x){
return (x^0x80)-0x80;
}
Cheers,
Timothy Robert Keal alias jargon

Image
Miser's House Anthology Project
User avatar
jargon
B&: This is not your blog
Posts: 208
Joined: Fri Dec 07, 2007 11:40 pm
Location: 480/85260
Contact:

Post by jargon »

Disch wrote:it actually makes perfect sense.

This works logically because when you clip to 8 bits, adding 255 is the same as subtracting 1. Example:

$63 - $01 = $62
$63 + $FF = $62 (really $162, but after clipping to 8 bits you're left with $62 because the $100 is lost)

2's compliment matches this logic *perfectly*. Because $FF is both 255 (unsigned), and -1 (signed).
now i understand why Laserbeak used that odd obsfucated method i reverse engineered from his textual questions in irc and on the board.
Cheers,
Timothy Robert Keal alias jargon

Image
Miser's House Anthology Project
Post Reply