Hi, I have searched for an answer to this (apparently) simple question on Google and Arduino so I am hoping that someone can explain how to do an assignment of bits either by structure or union and how to access / manipulate them.
I am simply trying to assign bits of a byte to use as a flag indicator to remember is a function was on or off when a subroutine is called.
I have stripped the code just to the bones as I keep getting out of scope errors in my current program. I have this little bit of code which I would like to use:
struct {
uint8_t _0 : 1; // bit position 0
uint8_t _1 : 1; // bit position 1
uint8_t _2 : 1; // bit position 2
uint8_t _3 : 1; // bit position 3
uint8_t _4 : 1; // bit position 4
uint8_t _5 : 1; // bit position 5
uint8_t _6 : 1; // bit position 6
uint8_t _7 : 1; // bit position 7
// total # of bits just needs to add up to the uint8_t size
} flagss;
uint8_t i;
void setup() {
// put your setup code here, to run once:
flagss == 0;
flagss._0 = true;
flagss._1 = true;
}
I used flagss in case flags is a reserved word by the way.
The first line of set up throws an error:
"sketch_dec21a:25:8: error: no match for 'operator==' (operand types are '' and 'int')
flagss == 0;
exit status 1
no match for 'operator==' (operand types are '<unnamed struct>' and 'int')"
I have tried a simple = sign same error type , but more errors generated:
"C:\Users\US6233\Documents\Arduino\sketch_dec21a\sketch_dec21a.ino: In function 'void setup()':
sketch_dec21a:25:10: error: no match for 'operator=' (operand types are '<unnamed struct>' and 'int')
flagss = 0;
^
C:\Users\US6233\Documents\Arduino\sketch_dec21a\sketch_dec21a.ino:3:10: note: candidate: <unnamed struct>&<unnamed struct>::operator=(const<unnamed struct>&)
struct {
^
C:\Users\US6233\Documents\Arduino\sketch_dec21a\sketch_dec21a.ino:3:10: note: no known conversion for argument 1 from 'int' to 'const<unnamed struct>&'
C:\Users\US6233\Documents\Arduino\sketch_dec21a\sketch_dec21a.ino:3:10: note: candidate: <unnamed struct>&<unnamed struct>::operator=(<unnamed struct>&&)
C:\Users\US6233\Documents\Arduino\sketch_dec21a\sketch_dec21a.ino:3:10: note: no known conversion for argument 1 from 'int' to '<unnamed struct>&&'
exit status 1"
no match for 'operator=' (operand types are '<unnamed struct>' and 'int')
If that line flagss == 0; or flagss = 0; is commented out it complies fine.
Is there a way to clear the whole byte flagss in one instruction, or do I need to explicitly clear or set required bits at run time?
Is there another way to declare this apparently simple variable?