[Qt-interest] Question about bitfields

Dan White ygor at comcast.net
Fri Jul 16 15:23:03 CEST 2010


Thanks, but I can see I did not include enough info in my original question.

I need to, for example, manipulate two boolean bits and a 14-bit signed integer stored in 2 bytes.

In plain old vanilla C, you say :

struct foo
{
unsigned flag1:1 ;
unsigned flag2:1;
signed   number:14
} ;

and there you go. 

“Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.”
Bill Waterson (Calvin & Hobbes)

----- Alex Strickland <sscc at mweb.co.za> wrote:
> Dan White wrote:
> 
>  > Are there any Qt classes that deal with bitfields ?  Or do I have to do it in 
> plain old vanilla C ?
> 
> If QFlags is not what you are looking for then perhaps vanilla C++:
> 
> vector< unsigned char > bitfield;
> 
> was the best I could come up with. Boost has better alternatives, as does the 
> new C++ standard library but I couldn't use them.
> 
> If the above will serve then this will be useful:
> 
> #ifndef BitOpsH
> #define BitOpsH
> 
> #define ClearBit( buf, i )      ( ( buf )[ ( i ) / 8 ] &= ~ ( 1 << ( i ) % 8 ) )
> #define SetBit( buf, i )        ( ( buf )[ ( i ) / 8 ] |= 1 << ( i ) % 8 )
> #define GetBit( buf, i )        ( ( buf )[ ( i ) / 8 ] >> ( i ) % 8 & 1 )
> 
> #endif
> 
> Regards
> Alex
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest





More information about the Qt-interest-old mailing list