how to access and set bits in a byte array?


+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2003
    Posts
    93

    Default how to access and set bits in a byte array?

    Hi all
    i have a byte array
    bArrRGBData VAR byte[4]

    i want to first check some individual bits across the entire array and then depending on their state set some other bits across the entire array individually depending on the outcome of the IF tests,
    but i can't figure out the syntax

    when i wrote
    bArrRGBData[0].1
    it was rejected
    so i tried
    bArrRGBData.1
    and PBP liked ok that but when i wrote
    bArrRGBData.8
    PBP was not happy either

    to state the overall goal (as there may be another way to skin this cat) there are 4 bytes in the array holding some pre-existing values,
    the byte is constructed of bit pairs
    i want to set a few bits in each array byte depending on the values of the bits in the bit pairs

    i realize i could copy to a byte variable then examine the bits there, then set the bits in that copy and then copy back to the array but that seems not only like a lot of work but a lot of code,
    any tips much appreciated as it seems there should be a better way

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,731

    Default Re: how to access and set bits in a byte array?

    bArrRGBData.0[offset]

  3. #3
    Join Date
    Nov 2003
    Posts
    93

    Default Re: how to access and set bits in a byte array?

    Quote Originally Posted by richard View Post
    bArrRGBData.0[offset]
    thanks
    ok i think i get that
    but
    bArrRGBData.0[8]
    starts at the 9th bit (?), but what does it return ?
    oh
    maybe i have a bit var
    bitvar = bArrRGBData.0[8]
    is that how to do it ?
    and is 9th correct? i.e. the [offset] starts counting from 0 ?
    thanks for your help

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    1,731

    Default Re: how to access and set bits in a byte array?

    pretty much
    bArrRGBData.0[8] is bit 0 of bArrRGBData[0] bit 0 + 8 bits , ie bArrRGBData[1] bit 0
    you can place it in any var but its value can only be 0 or 1
    can also be used as a logical value ie.

    if bArrRGBData.0[8] then dosomething
    else
    doanotherthing
    endif

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    1,731

    Default Re: how to access and set bits in a byte array?

    just a warning pbp does not ckeck the boundary conditions of this type of indexing
    if you were to
    go
    bArrRGBData.0[41] =1
    there is no error message and anything in the next ram/register position from bArrRGBData[4] would be wrecked

  6. #6
    Join Date
    Nov 2003
    Posts
    93

    Default Re: how to access and set bits in a byte array?

    Quote Originally Posted by richard View Post
    pretty much
    bArrRGBData.0[8] is bit 0 of bArrRGBData[0] bit 0 + 8 bits , ie bArrRGBData[1] bit 0
    you can place it in any var but its value can only be 0 or 1
    can also be used as a logical value ie.

    if bArrRGBData.0[8] then dosomething
    else
    doanotherthing
    endif
    thanks for you help, am removing some rust here due to inactivity
    what bothered me for a bit (pun intended) was whether this would by syntax return a single bit, i realize now that the .0 portion says that and explaining that bArrRGBData.0[8] is equivalent to bArrRGBData[1] bit 0 is just what i needed
    i also understand the risk about overrunning the allocation

  7. #7
    Join Date
    Nov 2003
    Posts
    93

    Default Re: how to access and set bits in a byte array?

    how about these cases
    MyWordArray VAR WORD[8]

    what does the following do exactly ?
    MyWordArray = 0
    does it set the first byte of the Array to 0 and leaves the rest as was ?

    what if there is a word variable used instead
    AWord VAR WORD
    MyWordArray VAR WORD[8]
    AWord = 0
    MyWordArray = AWord
    now are the first 2 bytes set to 0 ?
    just curious ....

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    1,731

    Default Re: how to access and set bits in a byte array?

    what does the following do exactly ?
    MyWordArray = 0
    I would not expect that to compile as "MyWordArray" is a pointer to a memory address.

    MyWordArray[0]=0 is the correct syntax to set the first word in the array to = 0
    MyWordArray[1]=0 for the next word

    AWord VAR WORD
    MyWordArray VAR WORD[8]
    AWord = 0
    MyWordArray = AWord

    MyWordArray[0] = AWord is whats required

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    1,731

    Default Re: how to access and set bits in a byte array?

    out of curiosity I had a look at what happens when you access an array just by using its name with no index []
    and pbp addresses the first var in the array as the type of var specified in the array declaration .
    I would not recommend the practice though

    Code:
    ****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 7/2/2014                                          *
    '*  Version : 1.0                                               *
    '*  Notes   : 16f1825                                                 *
    '*          :                                                   *
    '****************************************************************
                                
    #CONFIG
       __config _CONFIG1, _WDTE_ON    & _FOSC_INTOSC   &_MCLRE_ON    &_PWRTE_ON 
       __config _CONFIG2,              _LVP_OFF 
    #ENDCONFIG
    
    
     
     
     
          DEFINE OSC 32
     
       osccon=$70    '32 mhz
             anselA=0        'dig i/o 
             ANSELC=0
          
           TRISA= %11111110
         APFCON0.7=0
                 
               
                  so var porta.0
    so=1
    pause 4000 
                 
                 myarry var word[8]
     
       myarry[0]=$ffff 
       myarry[1]=$ffff  
       serout2 so,84,[ "ready ",13,10]      
       main:      
        pause 4000       
       
        
       
        
        serout2 so,84,[ #myarry[0]," ",#myarry[1],13,10] 
        
        
       
        
        myarry=0
        
        serout2 so,84,[ #myarry[0]," ",#myarry[1],13,10]  
      goto main
    result

    ready
    65535 65535
    0 65535
    Last edited by richard; - 14th September 2014 at 03:35. Reason: clean up code

  10. #10
    Join Date
    Nov 2003
    Posts
    93

    Default Re: how to access and set bits in a byte array?

    thanks Richard, that is what i wanted to know, yes agreed is probably best to avoid

Similar Threads

  1. How do I check (or set) a group of consecutive bits within a byte?
    By HankMcSpank in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 2nd April 2012, 08:13
  2. Problem with bits in byte array....
    By HenrikOlsson in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 19th October 2011, 06:27
  3. Access array in PBP and .asm
    By JEC in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 6th September 2008, 00:35
  4. reading first four bits of a byte
    By kamet in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd January 2007, 08:50
  5. Word array behaving like byte array.... wierd
    By forgie in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 2nd August 2005, 15:43

Posting Permissions

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