PDA

View Full Version : how to access and set bits in a byte array?



dsicon
- 8th September 2014, 04:29
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

richard
- 8th September 2014, 04:48
bArrRGBData.0[offset]

dsicon
- 8th September 2014, 05:04
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

richard
- 8th September 2014, 05:52
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

richard
- 8th September 2014, 05:59
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

dsicon
- 9th September 2014, 16:59
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

dsicon
- 13th September 2014, 16:53
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 ....

richard
- 14th September 2014, 01:35
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

richard
- 14th September 2014, 03:31
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


************************************************** **************
'* 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

dsicon
- 14th September 2014, 19:38
thanks Richard, that is what i wanted to know, yes agreed is probably best to avoid