PDA

View Full Version : Indexing the bits of a variable



manxman
- 19th April 2006, 21:52
This may seem trivial to the more experienced. I want to index each bit of a variable (e.g. MyVariable) and put it on a port. The compiler does not allow:

FOR BitCount=0 TO 15
PortC.1=MyVariable.BitCount
NEXT BitCount

Why not? Instead I have to do it this messy way:

Temp=MyVariable
FOR BitCount=0 TO 15
IF BitCount>0 THEN Temp<<1
PortC.1=Temp.15
NEXT BitCount

Any suggestions for a cleaner way, folks?

paul borgmeier
- 19th April 2006, 22:09
See this Melanie post

http://www.picbasic.co.uk/forum/showthread.php?t=544

then try this

FOR BitCount=0 TO 15
PortC.1=MyVariable.0(BitCount)
NEXT BitCount

Paul Borgmeier
Salt Lake City, Utah
USA

manxman
- 20th April 2006, 09:18
Paul, thanks for that, and also to Melanie for her excellent article about this topic. Although the syntax 'Variable.0(other variable)' does work, it seems a bit illogical by PBP standards! It's a pity that the User Manual does not refer to this trick.