Absolute value operator
O.A.A.
Posts: 25
Subject : Absolute value operator
In the simple example below when X is assigned a positive value Pin14
toggles as expected. When X is assigned a negative value
regardless of the absolute value" statement X := ||X" Pin15 toggles.
Apparantly absolute value statement does not store the absolute value
of X (+15) back into X. Can someone explain how to make the absolute
value operator effective !
Thanks,
O.Ardali
{{Output.spin}}
VAR
· word X
PUB Main(Pin, Delay, Count)
· X := -15
· X := ||X
· case X
··· 0..32768···· :·
····· Toggle(14, 3_000_000, 10)
··· 32768..65536 :
····· Toggle(15, 3_000_000, 10)········
···
PUB Toggle(Pin, Delay, Count)
· dira[noparse][[/noparse]Pin]~~····················
· repeat Count···················
··· !outa[noparse][[/noparse]Pin]···················
··· waitcnt(Delay + cnt)·············
In the simple example below when X is assigned a positive value Pin14
toggles as expected. When X is assigned a negative value
regardless of the absolute value" statement X := ||X" Pin15 toggles.
Apparantly absolute value statement does not store the absolute value
of X (+15) back into X. Can someone explain how to make the absolute
value operator effective !
Thanks,
O.Ardali
{{Output.spin}}
VAR
· word X
PUB Main(Pin, Delay, Count)
· X := -15
· X := ||X
· case X
··· 0..32768···· :·
····· Toggle(14, 3_000_000, 10)
··· 32768..65536 :
····· Toggle(15, 3_000_000, 10)········
···
PUB Toggle(Pin, Delay, Count)
· dira[noparse][[/noparse]Pin]~~····················
· repeat Count···················
··· !outa[noparse][[/noparse]Pin]···················
··· waitcnt(Delay + cnt)·············
Comments
-15 is $FFFFFFF1. When stored in X, it's $FFF1. ||X is still $FFF1 which is a positive 32 bit number.
You can force sign extension of a 16 bit value by using the "~~" operator as a prefix. "X := ||X" becomes "X := || ~~X"