I know this is really a general C question rather than a mspgcc-specific
one, but it is fairly relevant to anyone working with the new bitfield
structures for I/O pins.

If I have a structure defined:
    typedef { char a : 1; char b : 1; char c : 1;} tFlags;

and variables:
    tFlags f, g, h;

what is the best way to get their union or intersection?  If tFlags had been
a plain char, I would write "f = g | h;" or "f = g & h".  But that is not
allowed when they are structures, nor is it possible to cast them to "chars"
directly.  My best solution at the moment is to use a macro:

#define ToChar(x) (*(char*)(void*)(&(x)))

ToChar(f) = ToChar(g) | ToChar(h);
ToChar(f) = ToChar(g) & ToChar(h);

This works fine, and produces optimal code (as far as I have seen, with -O2
active) - there seems to be no problem keeping everything in registers even
though the macro messes around with pointers, which is very impressive.

However, if anyone has any ideas of how to do this without such casts, I'd
be very interested to hear.

mvh.

David



Reply via email to