Re: bitfield advantages

From: Graham Gilmore (gilmore@gmgate.vircom.com)
Date: 03/11/96


On Mon, 11 Mar 1996, Jaco van Iterson wrote:

> 
> 
> On Sat, 9 Mar 1996, Ben Greear wrote:
> 
> > 
> > 
> > I was wondering if you can use the | operator on two identicle
> > bit-fields, identical meaning that they are of the same type.
> > 
> > If so, then they could be very usefull, but if I had to overload the | operator,
> > then I see definate performance problems as apposed to using
> > regular old ints or long ints.
> > 
> > All my c and c++ books are very sparse on info pertaining to bitfields...
> > 
> > perhaps I'll go buy another one :)
> > 
> > 
> > Ben Greear
> > 
> 
> A bitfield is just a small integer so you can use the | operater on that, but
> you probebly mean to use the | operator on a structure with bitfields in it.
> This indeed has to be overloaded or done by a macro or function.
> The operation will be a little slower but I don't see any problems with
> the performance. The | operation is used only once in the bitfield code,
> in affect_join if I'm correct.
> Setting or resetting bits with bitfields doesn't need the | operation anymore,
> you can just set the flag 0 or 1 by using =    (affect.blind = 1;)
> 

	Instead of declaring the bitfield as a struct, you could use a 
union instead, and have one of your union members as a single int or long 
(or long long, if you have a large bitfield.. :);  that way you can use 
the | operator on the whole thing in one shot without overloading, while 
still being able to access your bitfields as structure members.  Of 
course, if you have something like 4 1/2 bytes of bits, this may seem 
wasteful.

	Graham Gilmore



This archive was generated by hypermail 2b30 : 12/07/00 PST