Bit manipulation???
James Long
Posts: 1,181
Ok guys....have a question....not really difficult...but I would like to know.
Is it possible to manipulate one bit within the propeller? Of the variable type.....not on the registers or pins.
Please bear with me....I'm a ladder logic person....not really a written code type.
I would like to use bits as variables/flags....but I don't see a way to use just one bit. Using a byte is just huge overhead.
Thanks,
James L
Is it possible to manipulate one bit within the propeller? Of the variable type.....not on the registers or pins.
Please bear with me....I'm a ladder logic person....not really a written code type.
I would like to use bits as variables/flags....but I don't see a way to use just one bit. Using a byte is just huge overhead.
Thanks,
James L
Comments
VAR long Bits[noparse][[/noparse]32] '1024 bits
PRI ReadBit(BitNum) : Bit
· Bit := Bits[noparse][[/noparse]BitNum >> 5] >> BitNum & 1
PRI WriteBit(BitNum, State)
· Bits[noparse][[/noparse]BitNum >> 5] |=·|< BitNum
· Bits[noparse][[/noparse]BitNum >> 5] ^=·|< BitNum &·not·State·
If you have only 32 bits, you could use the above code, but get rid of the [noparse][[/noparse]BitNum >> 5] indexes:
VAR long Bits '32 bits
·
PRI ReadBit(BitNum) : Bit
· Bit := Bits >> BitNum & 1
·
PRI WriteBit(BitNum, State)
· Bits |=·|< BitNum
· Bits ^=·|< BitNum &·not·State·
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
Chip I know I could have over 32 bit as flags.....but really doubtful. I use them alot ...but not in huge amounts.
I'll have to read up on your method of affecting them.....but I think I get it.
James
If you'd prefer using a bit number, you can use the "|<" operator. Setting Bit 22 is "x |= |<22". Clearing Bit 22 is "x &= !|<22". Testing Bit 22 to see if it's set is "(x & |<22) <> 0". Toggling Bit 22 is "x ^= |<22" and so on.
Mike
x := !|<22
Sid
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Don't have VGA?
Newzed@aol.com
·
Bit := VSCL[noparse][[/noparse]BitNum]···· 'read a bit
VSCL[noparse][[/noparse]BitNum] := Bit···· 'write a bit
!VSCL[noparse][[/noparse]BitNum]···········'toggle a bit
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
Post Edited (Chip Gracey (Parallax)) : 10/20/2006 6:22:52 PM GMT
Now that was very helpful.......Thanks.
Mike,
Thanks for the suggestion.
James L
"x := !|<22" doesn't mean what you think it does. The "!" is a logical "NOT" operator so "|<22" is $00400000 and "!|<22" is $FFBFFFFF, so your statement sets x to $FFBFFFFF.
Mike
You're right. It would need to be:
x ^= |<22········ 'xor $00400000 into x
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.