how bit field works?
typedef struct
{
unsigned char a:4;
unsigned char b:3;
}BITMAP1;
typedef struct
{
unsigned int a:4;
unsigned int b:3;
}BITMAP2;
void main()
{
cout<<sizeof(BITMAP1)<<endl;
cout<<sizeof(BITMAP2)<<endl;
}
The output is that sizeof(BITMAP1)=1, sizeof(BITMAP2)=4. why? how does bit field work?
[393 byte] By [
greghua] at [2007-11-19 6:59:07]

# 1 Re: how bit field works?
This has to do with how the compiler is organizing memory internally. It typically aligns structures in different sized byte allocation units. This is because it is a more efficient (faster) use of the memory manager.
This process is called packing, controlled by the compiler's pack pragma.
more details about this are here (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vccelng4/html/elconStorageandAlignmentofStructures.asp)
Hope this helps,
- Nigel
# 2 Re: how bit field works?
This is a so-called bit field and is a special case of a structure. In those structures the amount of bits for every component of it is separated with the : X.
1. Bit fields may be used inside a structure to pack several members into a single word of memory:
struct Foo
{
unsigned sevenbits : 7;
unsigned fourvals : 2;
};
creates a structure with two members: 'sevenbits' which can hold values between 0 (binary: 0000000) and 127 (binary: 1111111), and 'fourvals' which can hold values between 0 (binary: 00) and 3 (binary: 11)
The bit field length may be a constant expression, similar to an array length specifier.
2. Bitfields are addressed just like other structure members:
sFoo.sevenbits = 32;
sFoo.fourvals /= 2;
3. Bitfields can be either signed or unsigned (though pre-ANSI compilers may only allow unsigned)
4. Bitfields may be unnamed, to act as padding between named fields:
struct Foo
{
unsigned sea : 3;
unsigned : 3;
unsigned see : 3;
};
5. The & operator cannot be applied to a bitfield, since a bitfield is only a subset of the bits in a word and thus has no address
6. Bitfields may be mixed in with other types in a structure
7. Some machine-level details of bitfields are implementation-defined:
Bitfields may be stored left to right (big-endian) or right to left (little-endian), depending on the machine architecture
Some compilers will allow bitfields to overlap a word boundary, others will silently add padding bits
The maximum size of a bitfield is implementation-defined
8. These implementation-defined aspects make data created with bitfields inherently nonportable between dissimilar machines
# 3 Re: how bit field works?
The output is that sizeof(BITMAP1)=1, sizeof(BITMAP2)=4. why? how does bit field work?
Well...simply because in the first structure the underlying type is 'unsigned char' which is 1 byte while in the second example the underlying type is 'unsigned int' which is 4 bytes...
# 4 Re: how bit field works?
This has to do with how the compiler is organizing memory internally. It typically aligns structures in different sized byte allocation units. This is because it is a more efficient (faster) use of the memory manager.
This process is called packing, controlled by the compiler's pack pragma.
Well...actually this hasn't less to do with the memory alignment but rather with the underlying type being used for the bitfield in this case...see my previous reply...
# 5 Re: how bit field works?
[QUOTE=Andreas Masur]Well...simply because in the first structure the underlying type is 'unsigned char' which is 1 byte while in the second example the underlying type is 'unsigned int' which is 4 bytes...[/QUOTE
Yes, I think it's because the underlying type. But why should the bit field implemented like this? see the two structure have no difference, Only 7 bits are used either it's "int" or "char" type.
# 6 Re: how bit field works?
Because the size of the struct will never be smaller than the largest element it contains.
The compiler is not smart enough to determine how you are going to use the structure (nor should it be).
If you define it as an unsigned long, and the compiler then optimizes this down to the size of a char (as you infer), when you insert or extract values (as the unsigned int you defined), you will see warnings.
Hope this helps,
- Nigel
# 7 Re: how bit field works?
see the two structure have no difference, Only 7 bits are used either it's "int" or "char" type.The two structures do have a difference: the type that the programmer specified as the base type of the bitfield. The compiler will not change that type. If you don't use all the bits available to you, the compiler will not "downgrade" the type - you will just end up with n inaccessible bits. It may well be that choosing a larger-than-needed type is deliberate: perhaps it's to allow for future expansion in a structure whose size mustn't change. If the compiler arbitrarily decided to allocate a char when you asked for an int in that case, then adding a few more bits to the bitfields could result in a nasty shock.