How to chane sign of var from + to -
AGCB
Posts: 327
Probably a dumb question.
I want to enter a method w/ a negative value. The calling method does not support negative numbers .
IE I need to get -5 from +5
Simply negating subtracts from zero(256) and leaves 251.
I've tried every way I can think of. I'm using SPIN
Thanks
Aaron
I want to enter a method w/ a negative value. The calling method does not support negative numbers .
IE I need to get -5 from +5
Simply negating subtracts from zero(256) and leaves 251.
I've tried every way I can think of. I'm using SPIN
Thanks
Aaron
Comments
var := -var
xnew:=5
xnew1:=-xnew
That did it!
Thanks much
In Spin you just need;
Yes, but negatives only work with longs.
This one of the reasons from the sign extend operator "~" (when used on left side of a variable) in Spin. It can convert a 16-bit negative number (like those read from a sensor) into a full 32-bit negative which will behave properly when doing math in Spin.
In generally, I try to use longs if I'm going to being do much math with the variable.
This is one of those very useful but weird and nonstandard things about Spin. Remember the hated ~ operator that clears or sets vars when used postfix? Used prefix, it extends the sign of a short variable to 32 bits for this sort of thing.
~bytevar = sign-extended byte variable value
~~wordvar = sign-extended word variable value
These take the high bit of the byte or word and extend that bit through the rest of the 32-bit value to make something that will work with signed 32-bit arithmetic.