Indexing the bits of a variable


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2006
    Location
    Isle of Man
    Posts
    32

    Cool Indexing the bits of a variable

    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?

  2. #2
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    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

  3. #3
    Join Date
    Feb 2006
    Location
    Isle of Man
    Posts
    32


    Did you find this post helpful? Yes | No

    Cool Accessing bits in a WORD variable

    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.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  3. Individual Variable bits
    By tazntex in forum General
    Replies: 4
    Last Post: - 31st January 2008, 18:27
  4. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  5. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts