Bits, Bytes Words and Arrays


+ Reply to Thread
Results 1 to 25 of 25
  1. #1
    Join Date
    Jul 2003
    Posts
    2,358

    Default Bits, Bytes Words and Arrays

    1. How can I reference a BIT in a BYTE?
    2. How can I reference a BIT in a WORD?
    3. How can I reference a BIT in a BYTE ARRAY?
    4. How can I reference a BIT in a WORD ARRAY?
    5. How can I reference a BYTE in a WORD ARRAY?


    We all know there’s eight Bits in a Byte, and sixteen Bits in a Word. Sometimes however we need to access a Bit in a Byte, or a Word, or in an Array by using an intermediate VARIABLE rather than a constant.


    1. How can I reference a BIT in a BYTE?

    We know that MyByte.0 references Bit 0 of MyByte, all the way up to MyByte.7 referencing Bit 7 of MyByte… but there’s another way of accessing the bits in a Byte (MyVar is a BYTE variable)…

    MyByte.0(MyVar)

    When MyVar is in the range 0-7, it will access Bits 0-7 of MyByte.

    Example to display all eight BITS of a BYTE sequentially, Bit at a time…

    For MyVar=0 to 7
    MyBit=MyByte.0(MyVar)
    LCDOut $FE,1,"Bit ",#MyVar,"=",#MyBit
    Pause 2000
    Next MyVar


    2. How can I reference a BIT in a WORD?

    We know that Myword.0 references Bit 0 of Myword, all the way up to MyWord.15 referencing Bit 15 of MyWord… but there’s another way of accessing the bits in a word (MyVar is a BYTE variable)…

    MyWord.0(MyVar)

    When MyVar is in the range 0-15, it will access Bits 0-15 of MyWord.

    Example to display all sixteen BITS of a WORD sequentially, Bit at a time…

    For MyVar=0 to 15
    MyBit=MyWord.0(MyVar)
    LCDOut $FE,1,"Bit ",#MyVar,"=",MyBit
    Pause 2000
    Next MyVar


    3. How can I reference a BIT in a BYTE ARRAY?

    Arrays are arranged in memory in BIT ORDER from the LEAST SIGNIFICANT BIT of the first Byte at the bottom end, to the MOST SIGNIFICANT BIT of the last Byte at the top end. Consider this…

    MyByteArray var BYTE [40]

    There are 40 Bytes in this array (0-39), each with eight Bits. That means this array has a total of 320 Bits (numbered 0-319 sequentially). All the Bits in this Byte Array can be referenced individually like so…

    MyByteArray.0(MyVar)

    Where MyVar in this instance is a WORD (because there’s more than 256 Bits in the array). When…

    MyVar=0 it references Bit 0 of MyByteArray(0)
    MyVar=7 it references Bit 7 of MyByteArray(0)
    MyVar=8 it references Bit 0 of MyByteArray(1)
    MyVar=15 it references Bit 7 of MyByteArray(1)
    All the way, when…
    MyVar=312 it references Bit 0 of MyByteArray(39)
    MyVar=319 it references Bit 7 of MyByteArray(39)

    If MyVar was a BYTE rather than a WORD, you will only be able to access Bits 0-255.


    4. How can I reference a BIT in a WORD ARRAY?

    Just like the Byte Arrays, Word Arrays are arranged in memory in BIT ORDER from the LEAST SIGNIFICANT BIT of the first Word at the bottom end, to the MOST SIGNIFICANT BIT of the last Word at the top end. Consider this…

    MyWordArray var WORD [25]

    There are 25 WORDS in this array (0-24), each with sixteen Bits. That means this array has a total of 400 Bits (numbered 0-399 sequentially). All the Bits in this Byte Array can be referenced individually like so…

    MyWordArray.0(MyVar)

    Where MyVar in this instance is a WORD (because there’s more than 256 Bits in the array). When…

    MyVar=0 it references Bit 0 of MyWordArray(0)
    MyVar=15 it references Bit 15 of MyWordArray(0)
    MyVar=16 it references Bit 0 of MyWordArray(1)
    MyVar=31 it references Bit 15 of MyWordArray(1)
    All the way, when…
    MyVar=384 it references Bit 0 of MyWordArray(24)
    MyVar=399 it references Bit 15 of MyWordArray(24)

    If MyVar was a BYTE rather than a WORD, you will only be able to access Bits 0-255.


    5. How can I reference a BYTE in a WORD ARRAY?

    As can be previously seen, all the bits in an array are stored sequentially in memory. It follows that the BYTES in a WORD ARRAY also follow sequentially. This means we can easily extract any BYTE we want to out of a WORD Array in a similar manner. Consider again…

    MyWordArray var WORD [25]

    There are actually 50 Bytes in this array (0-49). The first byte (0) being the LOWBYTE of MywordArray(0), and the last byte (49) being the HIGHBYTE of MyWordArray(24). Using a similar manner to Bit access, we can access all fifty Bytes of this Word array like so…

    MyWordArray.Lowbyte(MyVar)

    Where MyVar is in the range 0-49 for my previously defined example Array of 25 Words.

    This is really handy, because we can use this method of loading or unloading Word Arrays from EEPROM for example very easily.

    Note that HIGHBYTE cannot be used in this instance, because it cannot access the LowByte of the first array element… ie..

    MyWordArray.HighByte(0)=MywordArray.LowByte(1)
    all the way to…
    MyWordArray.HighByte(48)=MywordArray.LowByte(49)

    More dangerously, MyWordArray.HighByte(49) will actually perform an unauthorised access on an unknown byte OUTSIDE the MyWordArray structure.

    Melanie

  2. #2
    anj's Avatar
    anj Guest


    Did you find this post helpful? Yes | No

    Default

    Gday Melanie
    I just read this post and found it quite interesting.
    On 6mar2004 i posted on the Picbasic Pro forum, re Accessing the PICs ports in subroutines.
    Particularly the ability to access ports not covered by the std 0..15 references.
    I never found anything on this in the manual, but it meant i could put direct port addressing into subroutines, by always addressing the required port relative to porta.0 .
    Maybe you could modify this post to include access to ports by the referential aliasing method as well?
    Just a thought.
    Andrew

  3. #3
    Join Date
    Dec 2004
    Location
    USA
    Posts
    36


    Did you find this post helpful? Yes | No

    Question words into byte array

    Found this interesting bit of ancient history. It covers all the options except the one I'm looking for. I need to put some words into a byte array.

    settings VAR byte[1]

    counter1.byte1 VAR settings[0]
    counter1.byte0 VAR settings[1]

    -does not appear to work

    Klaus

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    6. How can I reference a WORD in a BYTE ARRAY?

    Let's say that you have a WORD located at bytes 3 and 4 of a BYTE array, and Byte 3 is the LowByte. You can use the EXT (external) modifier to assign it to a WORD variable.


    MyByteArray VAR BYTE[10]

    MyWord VAR WORD EXT
    @MyWord = _MyByteArray + 3

    DT

  5. #5
    Join Date
    Dec 2004
    Location
    USA
    Posts
    36


    Did you find this post helpful? Yes | No

    Default

    Thanks Darrel,

    Your example will put the low byte into the array. How do I get the high byte in?

    MyByteArray VAR BYTE[10]
    MyWord.byte0 VAR MyByteArray[3]

    or

    MyByteArray VAR BYTE[10]
    MyWord.byte1 VAR MyByteArray[3]

    will work as well.

    I can read in either the high or low byte, but not both of them.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    It creates an Aliased WORD variable, inside the BYTE array.

    When you assign a value to the WORD variable, it puts both BYTEs in the array in a single operation.

    MyWord = $1234

    will make MyByteArray look like this

    [00, 00, 00, 34, 12, 00, 00, 00, 00, 00]

    HTH
    DT

  7. #7
    Join Date
    Dec 2004
    Location
    USA
    Posts
    36


    Did you find this post helpful? Yes | No

    Default

    Thanks again Darrel. That's almost too slick to be true. When you start throwing around those "@" signs it becomes a little mystical for me.

    I will try it and let you know.

  8. #8
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    136


    Did you find this post helpful? Yes | No

    Thumbs up Thank You

    After searching the whole forum for an hour or so i stumbled upon this page.
    Finally i can dynamically access bits of a variable. This has really helped me a lot.
    Thank you Melanie .

  9. #9


    Did you find this post helpful? Yes | No

    Default

    Isn't this information in the PicBasic Pro Compiler doc's... something like "Variables", "Aliases", "Arrays" and so forth...?

    Mike Tripoli

  10. #10
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mtripoli View Post
    Isn't this information in the PicBasic Pro Compiler doc's... something like "Variables", "Aliases", "Arrays" and so forth...?

    Mike Tripoli
    Probably most of it, Manuals and Data sheets are written by engineers, FOR engineers, and some of us NON engineers have a little trouble getting our heads around them, SO an experienced Advisor / Professor / Engineer / Guru / or even Lay person who knows, lending guidance is appreciated, and welcomed even if some of see it as too basic. You are new here, Welcome, Your statement tells me you might be really knowledgeable. I hope so, we can always use more people who are. I am not, but I help when I can. I learn something here nearly every time I come in. Every newbie can figure out a different way to "stall out" and I have seen some pretty unusual new ideas (that often do not work ) but they challenge me to think, and learn.
    Again Welcome Mike Tripoli.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  11. #11


    Did you find this post helpful? Yes | No

    Default

    I'm not "new" here; I just don't get into every little thing that's posted.

    Data sheets and manuals are not written "by engineers for engineers"; that's nonsense. They are written for those knowledgeable in the discipline. If you have not taken the time to make yourself knowledgeable then it's on you. If you don't understand the information in the manual then get a basic book on electronics and educate yourself. There is nothing in the manual that anyone with a basic understanding of electronics wouldn't understand.

    Contrary to what (it appears) many people on this forum think, electronics and microcontroller programming isn't some "plug 'n play" hobby. It requires a great deal of time to gain the knowledge to succeed. Yes, I am knowledgeable in the field. This didn't happen by accident; I've spent a great deal of time reading books, manuals and data sheets. I began with the fundamentals and worked up from there. Once you spend the time you'll begin to see that the "unusual new ideas" are neither "unusual" or "new" and are in fact rubbish from people that haven't spent the time up front to know what they are talking about.

    I feel so sorry for tech support guys; I really do.

    Mike Tripoli

    P.S. Your little "quote" about Communism is complete crap.
    Last edited by mtripoli; - 1st February 2010 at 19:31. Reason: nonsense

  12. #12
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mtripoli View Post
    I'm not "new" here; I just don't get into every little thing that's posted.

    Data sheets and manuals are not written "by engineers for engineers"; that's nonsense. They are written for those knowledgeable in the discipline. If you have not taken the time to make yourself knowledgeable then it's on you. If you don't understand the information in the manual then get a basic book on electronics and educate yourself. There is nothing in the manual that anyone with a basic understanding of electronics wouldn't understand.

    Contrary to what (it appears) many people on this forum think, electronics and microcontroller programming isn't some "plug 'n play" hobby. It requires a great deal of time to gain the knowledge to succeed. Yes, I am knowledgeable in the field. This didn't happen by accident; I've spent a great deal of time reading books, manuals and data sheets. I began with the fundamentals and worked up from there. Once you spend the time you'll begin to see that the "unusual new ideas" are neither "unusual" or "new" and are in fact rubbish from people that haven't spent the time up front to know what they are talking about.

    I feel so sorry for tech support guys; I really do.

    Mike Tripoli

    P.S. Your little "quote" about Communism is complete crap.
    OOOO!!!!!!
    An attitude!!!

    This forum is about helping people. Maybe you have never needed help in understanding or have never ask a question???

    Yes, there are many who come here looking for a copy/paste solution. They get "weeded".
    There are many who do not have the years behind them to have learned.
    There are many that come from another language or discipline also. Are they to be ridiculed?

    IF
    you think the people truly trying to learn are beneath you
    THEN
    BUZZ OFF!!!!
    ELSE
    never mind
    just
    BUZZ OFF...
    Dave
    Always wear safety glasses while programming.

  13. #13
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hi Mike,
    Opinions are what makes for horse races. Everyone starts out as a newbie, and people learn at different rates. Those are Facts, added to my opinion above. You have a great day! I choose not to argue.
    JS
    Edit: Melanie did this forum a service with this and nearly all of her posts, I say nearly, because Nobody is ever always or never.
    Last edited by Archangel; - 2nd February 2010 at 01:07.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  14. #14
    Join Date
    Mar 2011
    Posts
    6


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    This is working in Pic Basic:

    For MyVar=0 to 15
    MyBit=MyWord.0(MyVar)
    LCDOut $FE,1,"Bit ",#MyVar,"=",MyBit
    Pause 2000
    Next MyVar

    But I can not get to work in Pic Basic Pro

    I need to read/write some bits from/to a word variable and this method was very useful.

    Any ideas?

    Many thanks,

    Ricard.

  15. #15
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    Hi,
    It works perfectly fine here. I had to add a # in the LCDOUT statementin order to have it actually display "1" or "0" but apart from that it works as I'd exect it to.
    Code:
    MyVAR VAR BYTE
    MyWord VAR WORD
    MyBIT VAR BIT
     
    MyWORD = %1010101010101010
     
    For MyVar = 0 to 15
      MyBit = MyWord.0(MyVar)
      LCDOut $FE, 1, "Bit " ,#MyVar, "=", #MyBit
      Pause 2000
    Next MyVar
    /Henrik.

  16. #16
    Join Date
    Mar 2011
    Posts
    6


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    Henrik,

    Thanks for your help. I not tried exaktly this (i have no LCD)

    I tried this:

    MR var word: H var byte

    for H=0 to 15
    MR.0[H]=1 'set bit H of MR
    next H

    This works fine in PicBasic but when compiling in PicBasicPro this bits never are set. Other things in my code (about 8k) works perfect. I have only problems when adressing bits form words or bytes

    Maybe I have a library instalation problem?

  17. #17
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    Hi,
    That is weird.... This code:
    Code:
    MR var word: H var byte
     
    MR = 0
    HSEROUT ["First, all zeros:",13]
    HSEROUT ["MR = ", BIN16 MR,13,13]
     
    MR=65535
    HSEROUT ["Then, all ones:",13]
    HSEROUT ["MR = ", BIN16 MR,13,13]
    
    for H=0 to 15
        MR.0[H]=0  'reset bit H of MR
    next H
    HSEROUT ["Next, all zeros again:",13]
    HSEROUT ["MR = ",BIN16 MR,13,13]
     
    for H=0 to 15
      MR.0[H]=1 'set bit H of MR
    next H
    HSEROUT ["And finally, all ones:",13]
    HSEROUT ["MR = ", BIN16 MR,13,13]
     
    Pause 10
    END
    Produces the following result on the serial terminal:
    Code:
    First, all zeros:
    MR = 0000000000000000
     
    Then, all ones:
    MR = 1111111111111111
     
    Next, all zeros again:
    MR = 0000000000000000
     
    And finally, all ones:
    MR = 1111111111111111
    The FOR-NEXT loop is copy-pasted from your post above. How do you know it's not setting the bit? Do you have serial line to which you can send the data as above? Is there any chanse something else in the program is resetting/changing MR before you have chanse to see it? What version of PBP? (I'm using 2.60)

    /Henrik.

  18. #18
    Join Date
    Mar 2011
    Posts
    6


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    Thanks,

    I tried to run the isolated code and it works. :S
    I'm sorry. I tested only the complete program before.

    Sure the problem is in the code between. the weird is it works compiled with PicBasic and not when compiled with PicBasicPro.

    I will check possible causes...

    Thanks again.

    Ricard.

  19. #19
    Join Date
    Mar 2011
    Posts
    6


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    Hello,

    I have more information.

    I started commenting all the lines between this code, resulting the same. MR value is 0.
    Next, I continued commenting all the lines outside this code. Result the same. MR value is 0.
    Finally, I decided to comment step by step my array variables.

    After 2 o 3 arrays commented I get MR=65535.

    I repeated this process commenting other variables randomly with the same result. After 2 o 3 steps MR=65535.

    My code is about 8k over a Pic16F877. Very fair but fits.

    My conclusion is PicBasicPro compiler needs more memory for variables than PicBasic.

    The weird is why the compiler gives no errors...

    Many thanks,

    Ricard.

  20. #20
    Join Date
    Feb 2007
    Posts
    55


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    ok... I've built an 8x8x8 LED cube using 18F452, I have encountered a problem with my array

    each of the 512 leds as 1 bit assigned to it from leddata array

    0 = bottom corner
    511 = the top corner of the cube


    leddata VAR BYTE[64]
    litled VAR WORD

    litled = 511
    leddata.0(lilted) = 1

    the above 2 lines lights up an led half way down the cube (I think it would correspond to 255)

    but if I write this:

    leddata.0(511) = 1

    the correct LED lights up at the top corner of the cube

    Any ideas?
    Last edited by wellyboot; - 13th June 2016 at 19:56. Reason: error

  21. #21
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,793


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    You have an error in the variable litled.

    leddata.0(lilted) = 1

    It appears as lilted.

    Ioannis

  22. #22
    Join Date
    Feb 2007
    Posts
    55


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    awww that was an autocorrect on here, not in my code

    changed litled to 'lit' still no change,

    I've changed my code now to address each led using X,Y,Z
    and it is working ok


    xl = 7
    zl = 7
    yl = 7
    GOSUB ledon

    ledon:
    ledonbyte = xl + (yl*8)
    ledtemp = leddata[ledonbyte]
    ledtemp.0(zl) = 1
    leddata[ledonbyte] = ledtemp
    RETURN

  23. #23
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    Hi,
    I had a similar issue a couple of years (5) ago. Like you I found that indexing using a constant works but indexing using a variable doesn't (for bits beyond 255). And you, just like me back then, didn't bother to check the manual...
    7.6.4 Accessing Arrays as Multiple Variable-Types
    It is possible for a single array to be declared with multiple names and for each name to be assigned a different variable type. Note that offsets for BITs are limited to a maximum value of 255.
    So, I Think Melanies post where she indicates that using a WORD as the pointer for indexing bits in a BYTE Array really isn't correct.

    Here's my thread.

    /Henrik.

  24. #24
    Join Date
    Feb 2007
    Posts
    55


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    Thanks Henik

    solved that one

  25. #25
    Join Date
    May 2013
    Location
    australia
    Posts
    2,379


    Did you find this post helpful? Yes | No

    Default Re: Bits, Bytes Words and Arrays

    The other option is a bit of asm code.
    note the code would be slightly different for a pic18
    Attached Files Attached Files
    Warning I'm not a teacher

Similar Threads

  1. LP Instant Interrupts Error
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 7th December 2009, 10:13
  2. how to store a very large amount of data
    By shaiqbashir in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 2nd April 2008, 07:05

Members who have read this thread : 5

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