your code is missing a name, but besides that it’s okay.
When you are writing a typedef, you create a new type definion. So you don’t have only unsigned chars or signed ints anymore but also mytype.
You are using a bitfield as a structure. With 8 bits (maximum allowed value for SDCC!) it needs the same space as an unsigned char and you can store up to 8 boolean values.
A Union is an either-or-thing. Than means, you can either ask ALL bits (and get the 8 bit number; it could also be [tt]unsigned char ALL[/tt] instead of an 8 bit struct) or you are asking the structure and can access every single declared byte. So the data inside the Union remains the same, just the outside form changes.
Hm, I know, it sounds complicated, but actually it’s very easy if you take a look at these example code snippets:
Your typedef & declaration should look like that:
// this is optional, but if you're used to booleans, it's a bit nicer:
#define TRUE 1
#define FALSE 0
// typedef
typedef union {
unsigned char ALL;
struct {
unsigned oneBit : 1;
unsigned theRest: 7;
}
} myType_t;
// declaration
myType_t myVar; // got a var 'myVar' of the type 'myType_t'
now here are some example of using these vars:
// usage
myType.ALL = 0x1; // set all at once
// or
myType.oneBit = TRUE; // set every bit
myType.theRest = FALSE;
unsigned char c;
c = myType.oneBit; // c is 1
c = myType.ALL; // c is 1
or another example:
myType.oneBit = 0;
myType.theRest = 1;
c = myType.ALL; // ALL should be 2, cause the second bit is 1 (the first, third - seventh are all 0)
// so you got: 0 0 0 0 0 1 0 => 0x2
myType.oneBit = 1;
c = myType.ALL; // ALL is now 3 => 0 0 0 0 0 1 1 => 0x3