Code: Select all
f(x){
return (x&0x80)?-((x&0x80)-(x&0x7f)):x;
} blargg made this even simpler:
Code: Select all
f(x){
return (x^0x80)-0x80;
}Moderator: Moderators
Code: Select all
f(x){
return (x&0x80)?-((x&0x80)-(x&0x7f)):x;
} Code: Select all
f(x){
return (x^0x80)-0x80;
}now i understand why Laserbeak used that odd obsfucated method i reverse engineered from his textual questions in irc and on the board.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).