|
I'm just going to tackle a couple of these questions right now:
1) Two's complement is one of the ways represent negative numbers. Basically, the easy way to calculate the 2's complement of a number is to flip all the bits and add 1. the number (00110101) = 53 has a 2's complement of -53 = (11001011). However, in our world of P-BASIC, (11001011) = 203 because P-BASIC will display numbers as positive intergers only.
This form of a number is useful when you want to subtract numbers. If you want to do A-B = A + (2's complement of B) This is what most people use when they create digital circuitry, and what most (not all) computer processors use. My advise is to stay away from this unless you must because it can get very confusing very quickly.
5) I think i understand what you are asking because i've run into the same problem a numebr of times. Understand that a variable (in our case mostly bytes (8 bits) are loaded into the Arithmetic unit which uses 16 bits to perform calculations. So if we were to take a byte, in binary, (10011100) and perform ~(10011100) and then save it back ot the same place the basic process would look something like this:
varName = (10011100)
varName = ~varName
Description of the 2nd line of code:
1) 10011100 is loaded into the Arithmetic Unit and becomes (00000000 10011100)
2) this word (16 bits) is then inverted and becomes (11111111 01100011)
3) the word value (11111111 01100011) is truncated to a byte and the new value of varName = (01100011)
if you were using the ~ operator in the middle of a calculation then you were adding a whole lot of 1's to the front thus making your calculations not work, unless you saved back to a varriable.
There is a simple way to fix this. if you were to subtract off the leading 1's then you would get the answer you are loking for. So in the case of a byte, using the expression: (~varName - 65280) This means you can bitwise invert the number without having to save.
65280 = (11111111 00000000)
I hope this helps.
Steve
|