Bit Operations in Spin
hylee101001
Posts: 48
I wrote following code. But the result is little odd as commented below; it looks the data is larger than 16 bits..? Could someone explain this? Thank you.
Oh, the object I used here may not work though.. So please just put spaces at the object name and delete usb.newline. Or, use other serial object...
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
obj
usb : "ParallaxSerialTerminal.spin"
VAR
word x, one, zero, r, temp
PUB main
usb.quickstart
x := %1111_1111_0000_0101'_1111_1111_0000_0101
one := %1111_1111_1111_1111'_1111_1111_1111_1111
zero := %0000_0000_0000_0000'_0000_0000_0000_0000
temp := %0000_0000_0000_0000'_0000_0000_1111_1111
'r := x >> 8 << 8 ' outcome is as expected, 1111_1111_0000_0000
r := x << 8 >> 8 ' outcome is NOT as expected, 1111_1111_0000_0101
' I expected 0000_0000_0000_0101
repeat
usb.clear
usb.bin(r,32)
if (r>0)
usb.newline
usb.str(string("Least significant byte of x equlas 1"))
else
usb.newline
usb.str(string("Least significant byte of x NOT equlas 1"))
usb.newline
waitcnt(cnt + clkfreq/10)
Oh, the object I used here may not work though.. So please just put spaces at the object name and delete usb.newline. Or, use other serial object...
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
obj
usb : "ParallaxSerialTerminal.spin"
VAR
word x, one, zero, r, temp
PUB main
usb.quickstart
x := %1111_1111_0000_0101'_1111_1111_0000_0101
one := %1111_1111_1111_1111'_1111_1111_1111_1111
zero := %0000_0000_0000_0000'_0000_0000_0000_0000
temp := %0000_0000_0000_0000'_0000_0000_1111_1111
'r := x >> 8 << 8 ' outcome is as expected, 1111_1111_0000_0000
r := x << 8 >> 8 ' outcome is NOT as expected, 1111_1111_0000_0101
' I expected 0000_0000_0000_0101
repeat
usb.clear
usb.bin(r,32)
if (r>0)
usb.newline
usb.str(string("Least significant byte of x equlas 1"))
else
usb.newline
usb.str(string("Least significant byte of x NOT equlas 1"))
usb.newline
waitcnt(cnt + clkfreq/10)
Comments
Also,
-Phil
Some of the operations only work correctly on longs.
so the intermediate result of the first shift is written into the 16bit variable.
Andy
or, more simply:
-Phil