Often now when writing code where I need flexibility with a certain variable I use a type I call multi as it can be multiple types.
It goes like this,
And can be used to define a variable,
Now myVar can be accessed in many ways,
myVar.i is a 32 bit unsigned integer
myVar.s is a 32 bit signed integer
myVar.lo is the lower 16 bits
myVar.hi is the higher 16 bits
myVar.neg is the sign bit
myVar.b0 to myVar.b3 are the individual bytes of the variable.
A 16 bit variation of this can also be very useful. So you can do things like,
Saves on casting, shifting, multiplying, adding etc.
Hope someone finds this useful.
Mike.
It goes like this,
Code:
typedef union{
uint32_t i;
int32_t s;
struct{
uint16_t lo;
uint16_t hi;
};
struct{
unsigned :31;
unsigned neg:1;
};
struct{
uint8_t b0;
uint8_t b1;
uint8_t b2;
uint8_t b3;
};
}multi;
And can be used to define a variable,
Code:
multi myVar;
Now myVar can be accessed in many ways,
myVar.i is a 32 bit unsigned integer
myVar.s is a 32 bit signed integer
myVar.lo is the lower 16 bits
myVar.hi is the higher 16 bits
myVar.neg is the sign bit
myVar.b0 to myVar.b3 are the individual bytes of the variable.
A 16 bit variation of this can also be very useful. So you can do things like,
Code:
myVar.b0=loByte;
myVar.b1=hiByte;
return(myVar.i);
Hope someone finds this useful.
Mike.