While some information is stored, it does not need to occupy a full byte, only a few or one bits. For example, when storing a switching volume, there are only 0 and 12 states, with one binary. In order to save storage space and make processing simple, C language also provides a data structure, called "bit field" or "bit segment". The so-called "bit field" is to divide the binary in one byte into several different regions and describe the number of bits per region. Each domain has a domain name that allows operations to be performed by domain name in the program. This allows you to represent several different objects in a bits field of one byte. The definition of a bit field and the description of a bit-field variable are similar to the structure definition, in the form of:
struct bit domain structure name
{bit field List};
Where the list of bit fields is in the form: type specifier bit domain name: bit field length
For example:
struct BS {int a:8; int b:2; int c:6;}; The description of a bit-field variable is the same as the structure variable description. Can be defined by the first description, at the same time define the description or direct description of the three ways. For example: struct BS {int a:8; int b:2; int c:6;} Data: Data is a BS variable, accounting for two bytes. Where bit domain A occupies 8 bits, bit domain B is 2 bits, bit field C is 6 bits. There are several explanations for the definition of bit fields: 1. A bit field must be stored in the same byte and cannot span two bytes. If one byte has enough space left to hold another domain, the bit field should be stored from the next cell. You can also intentionally make a field start from the next unit. For example: struct BS {unsigned a:4 unsigned:0/* airspace */unsigned b:4/* from the next unit start storage */unsigned c:4} in this bit domain definition, a is the first byte of 4 bits, the last 4 bits filled 0 means not used, b from the second Byte starts, occupies 4 bits, and C occupies 4 bits. 2. A bit field can have no bit domain name, it is only used to fill or adjust the location. Nameless bit fields are not available. For example: struct k {int a:1 int:2/* The 2-bit cannot use */int b:3 int C:2};
From the above analysis, we can see that the bit field is essentially a type of structure, but its members are assigned by binary.
and
1: Pointer type variable cannot specify the number of digits taken
2. When declaring a member variable, you can use the variable name: bit number;
To determine the number of bits that the value of a member variable of a struct type occupies, and if in practice the value of the variable exceeds the number of bits declared when it is declared, the overflow portion is lost.
Example:
#include <cstdlib> #include <iostream>using namespace std;struct bitvariable { unsigned a:2; unsigned b:3; unsigned:0; unsigned c:6; } Bitvariable1;int Main (int argc, char *argv[]) { bitvariable BV1; bv1.a=2; Ten bv1.b=8; bv1.c=86; 1010110 cout<<bv1.a<<endl; Output 2 <===> 10B cout<<bv1.b<<endl; Output 0 <===> 1000B cout<<bv1.c<<endl; Output <===> 10110B cout<<sizeof (bitvariable) <<endl;//output 8. int 32-bit machine occupies 4 bytes. If unsigned:0 is removed, then 4 is output here. System ("PAUSE"); return exit_success;}
Sequence initialization of structures
typedef struct _DATA_T { int A; int b;} data_t;data_t data = { . A = ten, . B = 20,};
The usual way to initialize a struct is to initialize it sequentially, such as: data_t data={10,20}. Feel very curious, if you are on the Internet Baidu, found that the Linux under the initialization of the struct can be used in order and disorderly sequence two ways, and chaos and two different forms. This paper summarizes the advantages and disadvantages of two types of struct initialization, and gives a complete test procedure.
2. Sequence initialization
In textbooks, C language structure initialization is in the order of the way, there is no way to deal with the chaotic sequence. The sequential initialization of the struct must be in accordance with the order of the members, indispensable, if the structure is larger, prone to errors, and the form is not intuitive, you can not see the values of each struct data member.
3, Disorderly sequence initialization
Chaotic initialization is a new addition to the C99 standard, a more intuitive way to initialize. In the case of sequential initialization, the sequence initialization is like its name, the members can be initialized in order, and only some members can be initialized, and the extensibility is better. The Linux kernel initializes the struct in this way.
There are two ways to initialize a random sequence, one is to use a dot (.) symbol, and one is to use a colon (:). Mode 1 is the C99 standard, and Mode 2 is the extension of GCC, and it is strongly recommended to use the first method.
4. Test procedure
/********************************* * Linux C language Structure initialization method * @author Anker @date: 2014/02/11 * **************************** /#include <stdio.h>//function pointer typedef int (*CACULATE_CB) (int a, int b);//struct definition typedef struct _OPER {int A; int b; CACULATE_CB Cal_func;} The oper;//addition function defines an int add (int a, int b) {return (a+b);} int main () {int ret = 0; Sequential initialization of struct 1 oper oper_one = {Ten, +, add}; Disorderly initialization struct 2 oper oper_two = {. b =. A =,. cal_func = &add,}; Disorderly initialization of the struct 3 oper oper_three = {cal_func:&add, a:40, B:20,}; ret = Oper_one.cal_func (Oper_one.a, oper_one.b); printf ("Oper_one Caculate:ret =%d\n", ret); ret = Oper_two.cal_func (OPER_TWO.A, OPER_TWO.B); printf ("Oper_two Caculate:ret =%d\n", ret); ret = Oper_three.cal_func (Oper_three.a, oper_three.b); printf ("Oper_three Caculate:ret =%d\n", ret); return 0;}
The test results are as follows:
Reference http://www.cnblogs.com/Anker/p/3545146.html
C language struct declaration using colons (placeholders) & c struct Random initialization