Posted Sat Feb 28, 2004 12:31 AM
Decimal to binary is simple. You just subtract a power of 2 from each result until you get down to the last 1/0. If the remainder of each result is not high enough to subtract the next lower power of 2, you'd stick a zero in that "bit".
In a single byte, the maximum decimal amount that can be stored is 255 - the value you'd have if all of the "bits" in that byte held a 1 instead of a zero. The binary number %11111111 equals the decimal 128+64+32+16+8+4+2+1. Subtracting a binary value from another binary value works the same way as decimal subraction...you just need to remember that you'd be working with base 2 (only using digits 0 and 1)numbers and not base 10 (using digits 0 to 9).
At least that is how it works when you are treating all of those 256 combinations as positive values.
Since you only have 256 possible combinations, that doesn't leave any room for negative values. The 65xx chip provides a way of using negative values by including instructions that will treat half of those combinations as being positive (decimal values 0 to 127), and the other half negative (decimal values 128-255). This is signified by the state of the leftmost bit. If this bit is zero, the number is positive. When it's 1, the number is negative. And then the remaining bits signify what you would subtract. For example, %01111111 is equal to 64+32+16+8+4+2+1 decimal, or 127... but %11111111 is equal to 64+32+16+8+4+2+1 decimal minus the leftmost bit (128). 127-128= -1.