A detailed explanation of the use of the C language bit domain and notes _c language

Source: Internet
Author: User

The definition of a bit field

Some information, when stored, does not need to occupy a full byte, but only a few or a bits. For example, when storing a switch quantity, only 0 and 12 states, with one binary can be. In order to save storage space and make processing simple, C language provides a kind of data structure, called "bit field" or "bit segment". The so-called "bit field" is to divide the binary in a byte into several different regions and describe the number of digits in each region. Each domain has a domain name that allows you to operate by domain name in your program. This allows you to represent several different objects in a single byte bits field.

The definition of a bit field and the description bit field of a bit field variable are similar to the structure definition in the form of:

struct bit domain structure name
{bit field list};
Eg:struct Weiyu {
         int a:2;
         int b:5;
         int  : 5;//This five-bit is airspace and cannot use    
         int c:3;
          }

Bit fields are easy to use, but need to be noted when used:

1 if the adjacent bit field field is of the same type and its bit width is less than the sizeof size of the type, the following field is 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 sizeof size of the type, the subsequent field starts with the new storage cell, with an offset of an integer multiple of its type size;

3 The total size of the entire structure is an integral multiple of the widest base type member size.

4 If the adjacent bit field field type is different, then each compiler's concrete implementation has the difference, VC6 adopts the compression way, dev-c++ adopts the compression way;

5 If the bit field field is interspersed with a non-bit field field, no compression is performed (not for all compilers)

Note: 4, 5 with the compiler has a greater relationship between the use of caution, as far as possible to avoid.

#include "stdio.h" void main (int argn, char *argv) {struct Test {unsigned a:10;
    unsigned b:10;
    unsigned c:6;
    Unsigned:2;//this two bytes can ' t use unsigned d:4;
  }data,*pdata;
  data.a=0x177;
  data.b=0x111;
  data.c=0x7;
  
  data.d=0x8;
  pdata=&data;
  printf ("data.a=%x data.b=%x data.c=%x data.d=%xn", pdata->a,pdata->b,pdata->c,pdata->d);//bit fields can use pointers  printf ("sizeof (data) =%dn", sizeof (data));
  4 bytes, the most commonly used condition struct testlen{char a:5;
  Char B:5;
  Char C:5;
  Char D:5;
  Char E:5;
  
  }len;   printf ("sizeof (len) =%dn", sizeof (LEN));
    5bytes Rule 2 struct testlen1{char a:5;
    Char B:2;
    Char D:3;
    Char C:2;
    Char E:7;
  }len1;  printf ("sizeof (LEN1) =%dn", sizeof (LEN1));
    3bytes Rule 1 struct testlen2{char a:2;
    Char:3;
    Char B:7; Long d:20;
    4bytes Char E:4;
  }len2; printf ("sizeof (LEN2) =%dn", sizeof (LEN2)); 12 regular 3,4,5, total length of 4 integer times, 2+3 accounted for the 1byte,b accounted for 1bye due to with long on it, 2+3+7 accounted for 4 bytes, the following D and E were optimized for a 4-byte
  struct testlen3{char a:2;
    Char:3;
    Char B:7;
    Long d:30;
    Char E:4;
  }len3; printf ("sizeof (LEN3) =%dn", sizeof (LEN3))//12 rule 3,4,5, total length 4 integer times, 2+3 accounted for 1bye because of it, 1byte,b accounted for 4 bytes, followed D for a 4 byte, In order to guarantee the E exclusive of a 4-byte with long

Summarize

The above is the entire content of this article, I hope the content of this article for everyone to learn or use C language can bring certain help, if there is doubt you can message exchange.

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.