Shop OBEX P1 Docs P2 Docs Learn Events
Spin - Accessing individual bits - sort of like Bit0 — Parallax Forums

Spin - Accessing individual bits - sort of like Bit0

DeskguyDeskguy Posts: 32
edited 2008-12-26 16:56 in Propeller 1
Hi,

New to Spin, so perhaps a dumb question (but I have looked through the manual and the forums).

Is there some sort of easy way to grab individual bits from a variable?

Pseudocode example:

TestBit1 := BigVariable.Bit1
TestBit2 := BigVariable.Bit2
...

Also:

BigVariable.Bit2 := %1
BigVariable.Bit7 := %0

Thanks in advance to anyone who can help point me in the right direction.

Best regards,

David

Post Edited (Deskguy) : 12/26/2008 4:18:21 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-26 16:07
    Not like in Stamp Basic. You have to use bit arithmetic which is very fast. For example:
    TestBit := (BigVariable & |< BitNumber) >> BitNumber  ' This extracts the 0 or 1 where BitNumber is the bit number needed (0-31)
    
    BigVariable |= |< BitNumber  ' To set the bit to one
    
    BigVariable &= ! |< BitNumber  ' To set the bit to zero
    
  • DeskguyDeskguy Posts: 32
    edited 2008-12-26 16:34
    Thanks Mike - a wee bit more difficult than in PBasic!

    Using the code that you have for setting the individual bits in "BigVariable", are there any potential traps that I should know about if I use that code to set all 8 bits separately, and then use "BigVariable" to do something?

    (BTW, trying to use this for a few purposes, but in part as a 2 dimensional array - where the individual bits represent the second dimension)

    Thanks again.
  • SRLMSRLM Posts: 5,045
    edited 2008-12-26 16:56
    I asked this a while ago too. I tend to use the simple bit shift and modulus operator. It's not as simple as mike's, but it works.
Sign In or Register to comment.