Re: incorrect handling of bit fields
- From: "Doug Harrison [MVP]" <dsh@xxxxxxxx>
- Date: Mon, 16 Jan 2006 23:34:51 -0600
On 16 Jan 2006 20:07:36 -0800, "coder1024" <coder1024@xxxxxxxxx> wrote:
>I'm attempting to use bit fields to alllow extracting data from a
>packet received from an external hardware device. The hardware device
>sends a binary packet using a defined field layout.
>
>I created a struct, using bitfields to define the various fields and
>the structure came out much larger than expected. I had used both
>"unsigned int" (for 12 bit fields), and "unsigned char" (for <= 8 bit
>fields). I found that the "unsigned int" fields were causing the
>problems. I replaced them with multiple "unsigned char" fields (one 8
>bit and one 4 bit) and had to resort to macros to put them back
>together.
>
>The MS documentation with Visual C++ v7.1 seems to indicate you should
>be able to do this (see "C++ Bit Fields" in the "C++ Language
>Reference").
>
>For example, I tried the below:
>
>struct Packet
>{
> unsigned char f1 : 4;
> unsigned char f2 : 4;
>};
>
>I then used sizeof(Packet) and this returned 1 byte as expected. I
>then added some "unsigned int" fields, resulting in the below.
>
>struct Packet2
>{
> unsigned char f1 : 4;
> unsigned char f2 : 4;
> unsigned int f3 : 4;
> unsigned int f4 : 4;
>};
>
>sizeof(Packet2) returns 5 bytes. I also tried the below.
>
>struct Packet3
>{
> unsigned char f1 : 4;
> unsigned char f2 : 4;
> unsigned int f3 : 12;
> unsigned char f4 : 4;
>};
>
>sizeof(Packet3) returns 6 bytes.
>
>Any ideas? For anything <= 8 bits, I can just use unsigned char, the
>problem comes in when trying to represent a 12 bit integral value. I
>seem to be stuck currently with just storing two unsigned char fields,
>one 8 bit one 4 bit and then assembling them together with code.
What were you expecting, and why? Bitfields are inherently non-portable,
and you can't expect f3 and f4 to reside in the same machine word. However,
the VC docs suggest that if you make f4 unsigned int, they will:
http://msdn2.microsoft.com/en-us/library/hx1b6kkd.aspx
<q>
Adjacent bit fields are packed into the same 1-, 2-, or 4-byte allocation
unit if the integral types are the same size and if the next bit field fits
into the current allocation unit without crossing the boundary imposed by
the common alignment requirements of the bit fields.
</q>
Note: Although you didn't indicate it, you must be using a packing option
to get those sizes; please don't use /Zp for this, as you're liable to mess
up other structs that expect default packing to be in effect.
--
Doug Harrison
Visual C++ MVP
.
- Follow-Ups:
- Re: incorrect handling of bit fields
- From: coder1024
- Re: incorrect handling of bit fields
- From: David Webber
- Re: incorrect handling of bit fields
- References:
- incorrect handling of bit fields
- From: coder1024
- incorrect handling of bit fields
- Prev by Date: Hungarian notation question
- Next by Date: Re: Hungarian notation question
- Previous by thread: incorrect handling of bit fields
- Next by thread: Re: incorrect handling of bit fields
- Index(es):
Relevant Pages
|