Summary of the size-end mode and bit Fields

Source: Internet
Author: User

I recently read the code and suddenly saw the following: I was shocked at the time. Apart from some of the MFC, I was the first to see that the class definition involved macros. In addition, I used to have very little contact with the size end and bit field, but I just saw it, and then looked at it in a rough way. I couldn't think of it, so I would like to summarize it.

class DownlinkControlField {public:#if defined(__BIG_ENDIAN_BITFIELD)    unsigned short res1:2;    unsigned short ack:2;    unsigned short res2:2;    unsigned short dir:1;    unsigned short sof:1;    unsigned short mf:1;    unsigned short packet_type:2;    unsigned short transparent:1;#elif defined (__LITTLE_ENDIAN_BITFIELD)    unsigned short ack:2;    unsigned short res1:2;    unsigned short transparent:1;    unsigned short packet_type:2;    unsigned short mf:1;    unsigned short sof:1;    unsigned short dir:1;    unsigned short res2:2;#endif};

I. Size ends

In short, the big end is high to low, and the small end is high to high. Later, I searched for the large and small terminals and found a story. In a story in Gulliver's Travels, the leaders of the little man State issued a command to stipulate that people must start from a little-end period when peeling eggs. This rule angered a group of people who felt that they should be stripped from the big-end, and then turned into disputes. People who support little-end are called Little-Endian, and those who support big-end are called bid-Endian. (From http://bbs.c114.net/viewthread.php? Tid = 376162)

In this way, we can understand that the big end is to put the high point first, put the low address, and the small end is to put the small one first, put the low address. In fact, foreigners are quite interesting. About the size of the end of a pretty good article http://my.oschina.net/alphajay/blog/5478? From = RSS

The header file defined by the size end is located in the/usr/include/Linux/typeoders directory under Ubuntu, one little_endian.h and one big_endian.h. the header file is also named C: \ Program Files \ Microsoft Visual Studio \ vc98 \ include directory, endian. h. Remember to add the-D option during compilation, such as-d1_little_endian_bitfield.

Sometimes the interview questions will be judged by the size. Feature applied to Union: all members share one bucket during union storage;The storage order of Union starts from the low address.. Therefore, if you determine that the CPU is the size of the C program is as follows:

 /*return 1 : little-endian, return 0:big-endian*/int checkCPUendian(){union {unsigned int a;unsigned char b;            }c;c.a = 1;return (c.b == 1);      }  

Ii. Bit Fields

When the storage space is precious, you sometimes need to save multiple objects in one machine word. The common method is to define a set of shielding codes corresponding to the location of the relevant bit, such

#define  KEYWORD 01#define  EXTERNAL 02#define  STATIC      04

In this way, access to these bits will taste shift operations, shielding operations, and complement operations. Although this method is simple and feasible, the C language also provides an alternative method to directly define and access the bit fields in a word, without the need for bitwise logic operations.

Struct bytedata {unsigned A: 2;/* segment A, occupies 2 places */unsigned: 6;/* unsigned segment, occupies 6 places, but cannot access */unsigned: 0;/* No-name segment, which occupies 0 digits. The next segment of the table starts from the bottom boundary */unsigned B: 10;/* segment B, 10 */int I;/* Member I, starting from the bottom boundary */} data;

For bit fields, note the following:

1) A field must be stored in the same storage unit (Word) and cannot span two units. If the unit space is not enough, the remaining space is not needed. The Unit will be stored from next month.

2) almost all attributes of a field have specific implementations (so it is similar to the case of different sizes on the top)

3) special width 0 can be used to force alignment on the next word boundary

4) A blank field (only one colon and one width) is used for filling (like a dummy element ?)

5) fields are not arrays and have no addresses. Therefore, the & Symbol cannot be used.

6) fields can only be declared as Int. To facilitate transplantation, you must explicitly declare whether the int type is signed or unsigned.

7) the bit segment name is not a variable name. For example, the first int I: 3 and then Int J: 5 are not in two separate int values. Therefore, when calculating the sizeof struct, pay attention to the following:

I used to think that the direction of a lot of questions was quite strange. Now I found that some engineering practices were applied, but I did not have this requirement when I tried to play with some small code.

Reference: http://www.cnblogs.com/xiaoliyu/archive/2008/11/18/1335711.html

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.