a simple question of arithmetic
agentile
Posts: 101
Hello,
· I am writing a program to demonstrate how do deal with signed integers, and how to know if and when there is an overflow condition.· So I have the following code
k VAR Byte
q VAR Byte
x VAR Byte
k=5
q=6
x=k-q
DEBUG DEC k," - ",DEC q, " = ",SDEC x,CR
And the value I get for x is 255, which is the two's complement form of -1.· But I have used the signed decimal modifier, and I would like to get -1 instead of 255.
So, if I write·DEBUG··SDEC k-q, the answer comes out as -1.· Also, if I declare my variables as Words, it also works properly.· So, why won't it work with Bytes?
thanks,
Andrew
· I am writing a program to demonstrate how do deal with signed integers, and how to know if and when there is an overflow condition.· So I have the following code
k VAR Byte
q VAR Byte
x VAR Byte
k=5
q=6
x=k-q
DEBUG DEC k," - ",DEC q, " = ",SDEC x,CR
And the value I get for x is 255, which is the two's complement form of -1.· But I have used the signed decimal modifier, and I would like to get -1 instead of 255.
So, if I write·DEBUG··SDEC k-q, the answer comes out as -1.· Also, if I declare my variables as Words, it also works properly.· So, why won't it work with Bytes?
thanks,
Andrew
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Do you have a Stamp Tester yet?
http://hometown.aol.com/newzed/index.html
·
"IF A < 0" is NEVER true.
This is not really a problem, though, you can say "IF A.Bit7 = 1 THEN" if you know that you always use 'A' in signed arithmetic.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
x = 5 - 6
DEBUG SDEC x
The value displayed on the screen for x varies depending on how x was declared.· If x is a byte, the screen displays 255; if x is a word, the screen displays -1.· Both answers are correct, numerically.· The only part of this which appears to be incorrect is the way DEBUG SDEC is handling x.··This proper display would be -1 because I have asked for a signed value.· If I just said DEBUG x, then I would have expected 255 just based on variable overflow.· So, why can't I use DEBUG SDEC with bytes?
thanks,
Andrew
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
x VAR Byte
x=-1
DEBUG SDEC x
The Stamp assigns the number %1111111111111111 to x, But since x is just a Byte, it holds %11111111. Then in the DEBUG, it temporarily puts x into a word variable %0000000011111111 and then prints that in twos complement, so you get 255.
There are situations where you might need to work with twos complement bytes, say, to save memory. Or, some sensors return their data in a twos complement format in 8 or 9 bits, and you have to extend the sign. For example, the DS1620 returns 9 bits with x.bit9 being the sign. So to turn that into a proper twos complement word you have to extend the sign:
x VAR Word
x.byte1 = - x.bit8
You can go ahead and do twos complement arithmetic on Bytes, but suppose your sign bit is x.bit7. Then extend sign before or during the printing:
What that math does, for either DEBUG, is fill in the extra ones, so that %000000001ddddddd becomes %111111111ddddddd, where d is an arbitrary binary 0 or 1.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Post Edited (Tracy Allen) : 1/7/2006 6:19:59 PM GMT
y.byte0 = x
without which y will not equal x, even with sign extended.
The same principle applies to most of the calculations in a Stamp. It internally works with a 16 bit variable. So if the starting variable is a byte, nib or bit, the resulting word has its most significant bits set to zeros. And if you assign the result to a smaller chuck (byte, nib, bit), then the most significant bits are dropped.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com