The precise solution of C language bit domain

Source: Internet
Author: User
Tags bitwise

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
A bit field definition is similar to a 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:

1 struct BS 2 {3     int A:8; 4     int B:2; 5     int c:6; 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:

1 struct BS 2 {3     int A:8; 4     int B:2; 5     int c:6; 6 }data;


Indicates that data is a BS variable, which accounts 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;    Unsigned b:4;    Unsigned c:4;}


In this bit domain definition, a occupies 4 bits of the first byte, and the next 4 bits fill 0 to not use, B starts with the second byte, occupies
4 Bits, C occupies 4 bits.
2. Because bit fields are not allowed to span two bytes, the length of the bit field cannot be greater than the length of one byte, that is, it cannot exceed the 8-bit binary.
3. 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:

1 struct k 2 {3     int A:1; 4     int :2; 5     int B:3; 6     int c:2; 7 };


From the above analysis, we can see that the bit field is essentially a type of structure, but its members are assigned by binary.

Second, the use of bit fields
Bit fields are used in the same way as struct members, in the general form of:
Bit domain variable name • Bit domain name
Bit fields allow output in a variety of formats.

1 Main () {2     structBS3         {4Unsigned A:1;5Unsigned b:3;6Unsigned c:4;7} bit,*pbit;8Bit.a=1;9bit.b=7;TenBit.c= the; Oneprintf"%d,%d,%dn", bit.a,bit.b,bit.c); Apbit=&bit; -Pbit->a=0; -pbit->b&=3; thepbit->c|=1; -printf"%d,%d,%dn",pbit->a,pbit->b,pbit->c); -}


In the example program, the bit-domain structure BS is defined, and the three-bit domain is a,b,c. The variable bit for the BS type and pointer variable pbit to the BS type are described. This means that a bit field can also use pointers. The 9, 10, and 113 lines of the program assign values to the three bit fields respectively. (Note that the assignment cannot exceed the allowed range of this bit field) the 12th line of the program outputs the contents of three fields in an integer format. The 13th line sends the address of bit field variable bit to pointer variable pbit. The 14th row assigns a value to bit field A as a pointer to 0. Line 15th uses the compound bitwise operator "&=", which is equivalent to: pbit->b=pbit->b&3 bit field B has a value of 7, and 3 for bitwise AND operation results of 3 (111&011=011, decimal value 3). Similarly, the compound bit operation "|=" is used in the 16th line of the program. The reason to have a basic knowledge of dialysis such a column, is to tell everyone in detail the truth, rough things who all understand, cultivation of internal strength for the master of the way.
There is a flaw in the previous content, but also refer to the following article:
C99 specifies that int, unsigned int, and bool can be used as bit field types, but almost all compilers extend this to allow the existence of other type types.

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, then the following word
The segment will be stored adjacent to the previous field until it cannot be accommodated;
2) If the neighboring bit field field has the same type, but its bit width is greater than the type sizeof size, then the following word
The segment begins with the new storage unit, and its offset is an integer multiple of its type size;
3) If the adjacent bit field fields are of different types, the specific implementations of each compiler differ, VC6 take the uncompressed side
, dev-c++ adopts compression method;
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.

typedef struct  aa{       unsigned char b1:5;       unsigned char b2:5;       unsigned char b3:5;       unsigned char b4:5;       unsigned char b5:5;} AA;

  


sizeof (AA) = 5; But it actually only uses 25 bits, or 4 bytes,

(1) typedef struct  aa{       unsigned int b1:5;       unsigned int b2:5;       unsigned int b3:5;       unsigned int b4:5;       unsigned int b5:5;} AA; (2) typedef struct  aa{       unsigned int b1:5;       unsigned int b2:5;       unsigned int b3:5;       unsigned int b4:5;       unsigned int b5:5;       unsigned int b6:5;       unsigned int b7:5;} AA;

  

(1) is 5 members, according to the first rule, a total of 25, according to rule fifth, that is, sizeof (AA) =4
Now add the members to 7, reference (2), according to the first rule, a total of 35, according to the fifth rule, that is, sizeof (AA) = 8,

Let's look at an example:

struct Test1{char a:1;char:2;long B:3;char c:2;}; int len = sizeof (test1);

  

For the above example, the value of Len should be 12. The explanation is as follows:

First, the longest type of bit width as the offset, the longest is a long type, accounting for 4 bits, so the different types should be 4 byte offset, that is, test1 should be an integer multiple of 4 bytes.

char a:1;//Use one byte to store

Char:2;//airspace. Because the type of the previous a is the same, and the bit width addition of the two bit fields is still less than 8 bits, it is still represented by 1 bytes

Long b:3; The bit width of the//long type is 4 bytes, which is different from the preceding char type, so 4 bytes are offset between B and a, and 3 bytes are automatically replenished between them

char c:2;//Because C and B are different types, offset by the longest long type of bit width in test1, so although Char only uses 1 bytes it is enough

But it still takes 4 bytes.

The total is 12 bytes.

struct S1 {int i:8;  int j:4;  int a:3;  Double b;  };  struct s2 {int i:8;  int j:4;  Double b;  int a:3;  };  printf ("sizeof (S1) =%dn", sizeof (S1));  printf ("sizeof (s2) =%dn", sizeof (S2));  Result:16, 24 The first struct S1 {int i:8;  int j:4;  int a:3;  Double b; };

  

Theoretically, the first is I in the position relative to 0, accounting for 8 bits of a byte, and then, J is in the position relative to a byte, because the number of bytes in a position is a multiple of 4 bits, so do not align, put there, and then a, to the position of the 3-bit multiple relationship, so to move a bit, Put down in 15 position, currently a total of 18 bits, the conversion is 2 bytes 2 bits, because the double is 8 bytes, so in the relative 0 if the position of 8 bytes is dropped, so the position from 18 to 8 bytes is ignored, directly placed in 8 byte position, therefore, The total is 16 bytes.

The precise solution of C language bit domain

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.