Variable Bit access
omgitsaliv55
Posts: 24
Hi,
I am still a newbie with the propeller but am at the point where I need to be able to access individual bits of a variable, and so far all I have seen with the objects I've found is that people just use tricks with binary shifting and things like that to do what they need done. Is there any way that I can access and change a variable's bits directly like you can with the Basic Stamp 2? I appreciate any and all help.
Thanks,
Patrick
I am still a newbie with the propeller but am at the point where I need to be able to access individual bits of a variable, and so far all I have seen with the objects I've found is that people just use tricks with binary shifting and things like that to do what they need done. Is there any way that I can access and change a variable's bits directly like you can with the Basic Stamp 2? I appreciate any and all help.
Thanks,
Patrick
Comments
The only place where you can access individual bits and groups of bits is in some of the special registers (like OUTA and DIRA). You can access the two 16 bit words in a 32 bit long or the 4 bytes in a long or the 2 bytes in a word, but that's it.
The only way to access individual bits or groups of bits in any variable is with the bit arithmetic operators (& | ^) and the shift operators. There is a bit shift operator (|<) that takes a bit number and produces a mask which helps (|<n is the same as 1 << n).
Since OUTB is not really used, some people have moved a value to OUTB, then changed bits there and moved the result back to a variable.
there's no equivalent to "rhino.HIGHBYTE.LOWNIB.BIT1"
The concept of the Basic Stamp is to have a highly sophisticated set of commands doing complex things for you
like SERIN, SEROUT, set a bit, clear a bit, etc. These commands have a very LIMITED possability of varifying the commands
there is no direct way to do this in SPIN.
The concept of SPIN is to have a BASIC set of commands pre-programmed in the propeller-tool (and the SPIN interpreter)
SPIN allows to define NAMED subroutines that can vary in much more ways to suit almost every different kind of requirements
This means you can add self-defined commands to the basic set to expand this basic command-set of the language SPIN
it's no big thing to define new commands like
inside the commands "Set_Bit_Of_Byte" etc.
you have to use the bitmanipulating commands out of the basic command-set of spin.
For setting a bit the BITWISE OR command for clearing a bit the bitwise xor-command
see attached archived democode
best regards
Stefan
Post Edited (StefanL38) : 9/20/2009 7:24:11 PM GMT
Thanks again,
Patrick
destvar := togglebit(sourceval, bitnum)
Where sourceval can be a variable or constant, including the variable destvar.
Then to test, say, bit 5 of rhino, you'd do something like:
To set bit 5 of rhino, you'd do:
And to clear bit 5,
-Phil