Reading single bit out of a byte (or word or long)
Litefire
Posts: 108
hey, say i have four sensors whose pin states i want to store into a single variable (sensor).
sensor := sensor1
sensor :+ sensor2 <<1
sensor :+ sensor3 <<1
sensor :+ sensor4 <<1
now, how do i read single bits out of that sensor variable? is there a way besides simple bit shifting?
~~Brian
sensor := sensor1
sensor :+ sensor2 <<1
sensor :+ sensor3 <<1
sensor :+ sensor4 <<1
now, how do i read single bits out of that sensor variable? is there a way besides simple bit shifting?
~~Brian
Comments
'Output sensor1 bits 0..3 onto P0
VSCL := sensor1
outa[noparse][[/noparse]0] := VSCL[noparse][[/noparse]0]
outa[noparse][[/noparse]0] := VSCL
outa[noparse][[/noparse]0] := VSCL
outa[noparse][[/noparse]0] := VSCL
'Input P0 into sensor1[noparse][[/noparse]0..3]
VSCL[noparse][[/noparse]0] := P0
VSCL := P0
VSCL := P0
VSCL := P0
sensor1 := VSCL
That code doesn't do anything practical, but it shows how you can (ab)use VSCL for any bitwise purpose. You can specify ranges of bits to: VSCL[noparse][[/noparse]3..0] or the reverse VSCL[noparse][[/noparse]0..3].
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
i assume this would be faster than bitwise shifts... could i set VSCL as an alias to sensor? so that when sensor changes VSCL changes along with it?
~~Brian
Chip Gracey
Parallax, Inc.
~~Brian