A bit segment (Bit-field) defines the space occupied by a member variable in a struct (or union) in bits. A struct (union) that contains a bit segment is called a bit segment structure. The use of bit-segment structure can not only save space, but also easy to operate.
A bit segment is defined in the following format:
Type [var]: Digits
which
Type can only be int,unsigned int,signed Int,char, unsigned char one of five types, digits indicates the number of bits that the bit segment occupies
The bit segment length digits cannot exceed the size of the data type that corresponds to the type, and if type is char, digits cannot exceed 8, and int digits cannot exceed 32
Bit segment name Var is an optional parameter that can be omitted
Definition of a bit field
A bit field definition is similar to a struct definition in the form of:
struct bit domain structure name
{
bit field List
};
Where the bit field list is in the form: Type [var]: Digits
For example:
1 structdata{2 3 intA: +;//bit segment A, 32-bit4 5 CharD |5;//bit segment B, 5-bit6 7 CharC:2;//bit segment C, accounting for 2 bits, and b stored in the same byte8 9UnsignedChar:6;//Nameless Segment, 6-bitTen One};
Note: A bit field can have no bit domain name, it is used only to fill or adjust location, nameless bit field cannot be accessed
Second, the use of the bit segment
The use of bit fields is the same as the use of struct members, the general form is: bit field variable name • Bit segment name, you can also define a pointer to the bit field p, and then the p-> bit segment name to use, you need to note:
① cannot take address operations on a bit segment
② if the bits number of bits is 0, the bit segment must be a non-fame segment, and the next bit segment will be stored from the next bit-segment storage unit
③ If a bit segment appears in an expression, an integer upgrade is automatically made to an int or unsigned int
When assigning a ④ to a bit segment, it is best not to exceed the maximum range represented by the bit segment, or it may cause unexpected results
Three, bit segment in memory storage mode
The primary purpose of using bit fields is to compress the storage, with the following approximate rules:
1) If the adjacent bit field field is of the same type and its bit width is less than the type sizeof size, the subsequent field will be stored next to the previous field until it cannot be accommodated;
2) If the adjacent bit field field is of the same type, but its bit width is greater than the type sizeof size, then the subsequent field will start from the new storage unit, and its offset is an integer multiple of its type size;
3) If the adjacent bit domain field type is different, the specific implementation of the compiler has a difference, by the experimental VC to take an uncompressed way, that is, even if the previous field has enough space to accommodate the latter field, the latter field is not currently a field of free bits, the previous field type size after the new bytes start to store;
4) If the bit field fields are interspersed with non-bit field fields, no compression is performed;
5) The total size of the entire struct is an integer multiple of the size of the widest base type member. :
The test procedure is as follows, Environment Vs3013,cfree 5.0
#include <stdio.h>#include<stdlib.h>structdata{intA: -;//bit segment A, 20-bit CharD |3;//bit segment B, accounting for 3 bits, does not and a compressed storage Char:0;//Anonymous bit segmentunsignedCharC:5;//bit segment C, 5-bit};intMain () {data test; Data*ptest; Ptest= &test; //two ways to access bit segmentsTEST.A = -; Ptest->b =5; //VC does not compress storage, occupies 8 bytes, and is an integer multiple of its widest base type member sizeprintf"sizeof (data):%d\n",sizeof(data)); //the test.b is automatically converted to integer at the output, 5 (101) High is 1, and the high-level continuous 1 is extended, and the value is 3printf"test.a =%d\t test.b =%d\n", TEST.A, ptest->b); System ("Pause"); return 0;}
The result of the operation is:
sizeof (data): 8
TEST.A = test.b = 3
Reference:
Http://www.cnblogs.com/dolphin0520/archive/2011/10/14/2212590.html
Http://blog.sina.com.cn/s/blog_3d8529400100istl.html
A brief analysis of C language bit field