What is the spin equivalent
Greg LaPolla
Posts: 320
in Propeller 2
In C you can do
var = ~var
but it doesn't work in spin 2. I tried
var := !var
and it makes 8 bits become 32 bits.
Comments
! is the correct operator. Spin does not have a way to recast variable size. I tested on a byte and then output 16 bits -- the upper eight bits were 0 (hence not affected).
here is the scenario:
lets say val = %10000000
in c val = ~val would yield %01111111
in spin2 val = !val would yield %11111111_11111111_11111111_011111111
Is var declared as a byte in a var section ?
No.
in PUB i initialize it as byte[@val] := $00
That will only happen, Greg, when your variable is a long. As I stated earlier, Spin cannot recast variables. If you declare your variable as a byte it will stay a byte -- and remember, locals are longs unless you explicitly define them as bytes or words.
Here's a screen-shot of my test.
I changed it to byte on the pub line. That fixed part of the problem.
val = %10000000
val = !val
now val = %1111111
here is the code
first debug in else prints out %10000000
second debug in else prints out %1111111
Looks like it flipped all the bits. Isn't that the right thing to do?
It's missing a 0 at the beginning of the second debug. the value after the !val should be %01111111
Debug may not be showing you that leading 0
Greg,
It is probably that the un-typed version of the ubin output of debug doesn't show leading zeroes to pad out a byte.
Try:
debug ("flipped ",ubin_byte(val))
to format the displayed output as a full byte.
There is an implied 0 at the beginning.
%1111111 = %01111111
var!
That will flip all bits.