A detailed introduction to the C-language segment _c language

Source: Internet
Author: User

Bit segments in the C language

A bit segment (Bit-field) is the space in a bit to define a member variable in a struct body (or consortium). A structure Body (consortium) containing a bit segment is called a bit structure. The use of bit-segment structure can save space, but also convenient operation.

The definition format for a bit segment is:

Type [var]: Digits

Where type can only be int,unsigned int,signed int three types (int type can not represent negative numbers depending on the compiler, such as VC in the int is the default is signed int, can represent negative). Bit-segment name var is an optional parameter, which can be omitted. Digits represents the number of bits that the segment occupies.

The definition of a bit-segment structure can be defined as the following code:

struct node
{
  unsigned int a:4;   Segment A, accounting for 4-digit
  unsigned int:0;   No fame segment, accounting for 0-bit
  unsigned int b:4;   Bit B, accounting for 4-bit
  int c:32;       Segment C, accounting for 32-digit
  int:6;       No fame segment, accounting for 6-bit
};

I. Use of the bit segment

Use a bit to note a few points:

1 The type of the bit segment can only be int,unsigned int,signed int three types, not char or floating-point;

2 The number of bits in the bits can not exceed the maximum number of digits represented by the basic type, for example, in VC, the int is 4 bytes, then the maximum can be 32 bits;

3 No fame paragraph can not be accessed, but will occupy space;

4 the location segment can not be taken to address the operation;

5 if the number of bits is 0, the segment must be a fame segment, the next segment from the next segment storage unit (where the bit segment storage unit is tested in VC environment is 4 bytes) to start storing;

6 If the bit segment appears in the expression, an integer upgrade is automatically made, automatically converted to int or unsigned int.

7 when assigning a value to a bit segment, it is best not to exceed the maximum range that can be expressed by the bit segment, otherwise it may cause unexpected results.

8 bits cannot appear in the form of an array.

Two. How the bit-segment structure is stored in memory

For the bit structure, the compiler will automatically optimize the storage space, there are several principles:

1 if a bit-segment storage unit can store all members of the lower segment structure, all members in a bit structure can only be placed in one bit segment storage unit and cannot be placed in a two-bit storage unit; If a bit-segment storage unit cannot accommodate all members of the lower-segment structure, The remaining bits are then stored from the next bit segment storage unit. (The size of the segment storage unit in VC is 4 bytes).

2 If there is only one unknown segment in a bit structure that occupies 0 digits, only 1 or 0 bytes of space (0 bytes in C, and 1 in C + +); otherwise, in any other case, the space occupied by a bit-segment structure is at least the size of a bit-segment storage unit;

Test program:


/* Test bit segment 201110.12*/ 
#include <iostream>
using namespace std; 

typedef struct NODE
{
  unsigned int a:1;   There is a 0-bit segment, at least 4Byte 
}s; 

typedef struct NODE1    //in C + + for 1 bytes of space, in C for 0 bytes 
{
  unsigned int:0;
} S1;

typedef struct NODE2
{
  unsigned int a:1;
  unsigned int:0;   The next bit is placed in a new bit-segment storage unit, so it accounts for 4+4=8byte 
  unsigned c:32.     
S2;

typedef struct NODE3
{
  unsigned int a:4;
  unsigned int:0;
  Int:6;        This bit segment is placed in a new bit segment storage unit 
  unsigned c:32;    Because of the 6+32>32, the segment C is also placed in a new bit-segment storage unit, so it accounts for 4+4+4=12byte 
}s3;

typedef struct NODE4 
{
  unsigned int a:1;
  Char b;        All members can be saved in a bit-segment storage unit, so the 4Byte 
  int c:1 is taken.
  int d:2;
  unsigned int e:2;
} S4;


int main (int argc, char *argv[])
{
  S4 S4;
  s4.a=1;
  s4.c=1;
  s4.d=2;       
  s4.e=3;
  printf ("%d%d%d%d\n", S4.A,S4.C,S4.D,S4.E);
  printf ("%d%d%d%d\n", sizeof (S), sizeof (S1), sizeof (S2), sizeof (S3), sizeof (S4));
  return 0;
}

The results of the execution are:

1-1-2 3
4 1 8 12 4
Please press any key to continue ...
When you print the individual bit segments of a S4, the results of the printing are different from the initial values assigned.

Because c only occupies 1 bits, then there is no data bit, at this point, the symbol expansion directly at the high level to add 1, so the printed result is-1;

Since d is 2 bits, when 2 is assigned to D, the contents in memory are 10, and then the symbol extension, the high level of 1, the 0XFF FF FF FE, then the true value is-2.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.