Menu

Array for BIT

Help
Gigi
2018-02-02
2018-02-03
  • Gigi

    Gigi - 2018-02-02

    Is it possible to create BIT arrays? It always gives me error
    Es: Dim Test(10) as bit

     
    • Anobium

      Anobium - 2018-02-02

      You cannot create an array of bits.

      But, you can an arrays of bytes and 'use' as an array of bits. Then set the bit using FnLsL and an code to determine the BitNum you are going to set and the Byte_adress. As in the buffer(10) use the following.
      buffer(Byte_adress) = buffer(Byte_adress) OR FnLSL( 1, BitNum)

       
  • Chris Roper

    Chris Roper - 2018-02-02

    Bit arrays are a bit of an anomaly (no pun intended) as every variable in any computer language is automatically an array of bits. When accessed as ASCII code we call 8 Bits a Char, an 8 bit Number is an Integer, a 16 bit number a word etc. When the individual bits, rather than the data structure are accessed most languages call them a Flag.

    GCBasic makes it easier than most other languages to access individual bits with the ‘Var.Bit’ form of reference.

    For example if you need to set Pin 7 of a PIC16F690 to High you can use the command:

    PortC.3 = 1 ‘ Set Pin7 - RC3 High
    

    Likewise if you need to save an interim Bit value you can do the same with Variables.

        MyFlag.4 = PortA.0 ‘Read switch on Pin RA0 and store result
    

    So in order to use a data structure as an array all we need is the ability to specify the bit Number and the bit State as variables, for that we can create a function that expands on what Evan posted:

    Sub SetBit(BitArray, BitNum, BitState)
      if BitState then
        BitArray = BitArray OR FnLSL(1, BitNum)
      else
        BitArray = BitArray And FnLSL(0, BitNum)
      end if
    end sub
    

    Using that function BitArray can be any variable, register or Port on the device, BitNum is the Bit to be changed and BitState is the the State (TRUE/FALSE - ON/OFF - 1/0) of the target Bit.
    BitNum and BitState may be specified as a constant, variable, expression or function.

    The line:

        SetBit(PortC, 1+2, 4)
    

    Will light an LED on Pin RC3 because 1+2 evaluates to 3, and 4 is NOT Zero so evaluates as TRUE.

    A complementary function would read the array:

    Function GetBit(BitArray, BitNum)
      Repeat BitNum + 1
        Rotate BitArray Right
      End Repeat
      GetBit = STATUS.C
    End Function
    

    Putting it all together as an test on a PIC16F690:

    #chip 16f690
    
    dim MyBitArray as byte
    
    Dir PortC Out
    
    MyBitNum   = 3
    MyBitState = 1
    
    SetBit(MyBitArray, MyBitNum, MyBitState)
    
    ' set or clear RC0 according to Bit Array value
    PortC.0 = GetBit(MyBitArray, MyBitNum) 
    
    end
    
    Sub SetBit(BitArray, BitNum, BitState)
      if BitState then
        BitArray = BitArray OR FnLSL(1, BitNum)
      else
        BitArray = BitArray And FnLSL(0, BitNum)
      end if
    end sub
    
    Function GetBit(BitArray, BitNum)
      Repeat BitNum + 1
        Rotate BitArray Right
      End Repeat
      GetBit = STATUS.C
    End Function
    

    So in conclusion GCBasic has got bit arrays in that every data structure is a bit array, the two functions above will allow you to access a byte as if it were an 8 element Bit Array passing variable or coinstants as you would any other type of array.

    Cheers
    Chris

     

    Last edit: Chris Roper 2018-02-02
  • Moto Geek

    Moto Geek - 2018-02-03

    @Chris Roper... I like this. Nice explanation!

     
    • Anobium

      Anobium - 2018-02-03

      @Chris. It would be very good if you could edit this for inclusion in the Help? Only change would to make make the document talk of the 'third party'. You up for this?

       
  • Gigi

    Gigi - 2018-02-03

    Thank you all,
    I'm trying to do a small multi-task for reading keyboard update display etc. trying to use as few ram as possible, is there any example already done?

     
    • Anobium

      Anobium - 2018-02-03

      Looked at your demos? ..\GCB@Syn\GreatCowBasic\Demos\KeyPad Solutions\Keypad_Demonstration_with_Hardware_Serial-16F1938.gcb and others in the same folder.

       
  • stan cartwright

    stan cartwright - 2018-02-03

    I like Anobiums idea more. I can work with a rotating mask ok for bit arrays.
    Nice thought Chris, I thought about it to but better to understand the idea and do one's own thing.
    Gigi, please elaborate "I'm trying to do a small multi-task for reading keyboard update display etc. trying to use as few ram as possible, is there any example already done?" interesting

     
  • Gigi

    Gigi - 2018-02-03

    @Anobium@Stan
    Great, as always I start something that has already been done.
    I saw the library: Time_Based_Task_Switcher_Library001.h It seems just face
    in my case. Truly great GCB
    MANY THANKS

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.