HI David,
Macro is fine. For example, gcc FP lib does the same trick.
(actually '#define ToChar(x) (*(char*)(&(x)))' is enough :)
Alternatively, you can do:
typedef union {
char reg;
struct {char a : 1; char b : 1; char c : 1;} bits;
} tFlags;
tFlags f, g, h;
then call
f.reg = g.reg | h.reg;
f.reg = g.reg & h.reg;
You'll get exactly the same result.
~d
P.S. it is better to declare bitfields as 'unsigned' cause zero extension is
much simpler if required. Also, attribute 'packed' might make sense.
On Monday 16 December 2002 16:15, David Brown wrote:
> 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
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:
> With Great Power, Comes Great Responsibility
> Learn to use your power at OSDN's High Performance Computing Channel
> http://hpc.devchannel.org/
> _______________________________________________
> Mspgcc-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users
--
/********************************************************************
("`-''-/").___..--''"`-._ (\ Dimmy the Wild UA1ACZ
`6_ 6 ) `-. ( ).`-.__.`) Enterprise Information Sys
(_Y_.)' ._ ) `._ `. ``-..-' Nevsky prospekt, 20 / 44
_..`--'_..-_/ /--'_.' ,' Saint Petersburg, Russia
(il),-'' (li),' ((!.-' +7 (812) 3468202, 5585314
********************************************************************/