Hi I have some problems with struct inside a union This is my code.
union{ unsigned char id_byte[2]; struct { unsigned char DLC:4; unsigned char RTR:1; unsigned int identifier:11; }del_element; }can_descriptor;
union{ unsigned char id_byte[2]; struct { unsigned int identifier:11; unsigned char DLC:4; unsigned char RTR:1; }del_element; }can_descriptor;
if I shifts, I need more code, becourse I need to shift different if i have a RTR bit set. I rather strongly suspect the opposite to be true. If the shift is, indeed, different depending on the status of some bits, that's impossible to model using bitfields alone, because the lengths, and thus by implication the positions, of bitfields inside a struct are fixed at compile time. It's much more transparent to have the argument of a shift operator be a variable or expression than having to do something like
if(rtr) my_union.rtr_set.somebit = 1; else my_union.rtr_clr.somebit = 1;