C++ Bit Fields

C++ Bit Fields

Visual Studio 2010
Other Versions
Classes and structures can contain members that occupy less storage than an integral type. These members are specified as bit fields. The syntax for bit-field member-declaratorspecification follows:
declarator  : constant-expression
Remarks
The (optional) declarator is the name by which the member is accessed in the program. It must be an integral type (including enumerated types). The constant-expressionspecifies the number of bits the member occupies in the structure. Anonymous bit fields — that is, bit-field members with no identifier — can be used for padding.
 Note
An unnamed bit field of width 0 forces alignment of the next bit field to the next type boundary, where type is the type of the member.
The following example declares a structure that contains bit fields:
// bit_fields1.cpp
// compile with: /LD
struct Date {
   unsigned short nWeekDay  : 3;    // 0..7   (3 bits)
   unsigned short nMonthDay : 6;    // 0..31  (6 bits)
   unsigned short nMonth    : 5;    // 0..12  (5 bits)
   unsigned short nYear     : 8;    // 0..100 (8 bits)
};
The conceptual memory layout of an object of type Date is shown in the following figure.
Memory Layout of Date Object

这里写图片描述

Note that nYear is 8 bits long and would overflow the word boundary of the declared type, unsigned short. Therefore, it is begun at the beginning of a new unsigned short. It is not necessary that all bit fields fit in one object of the underlying type; new units of storage are allocated, according to the number of bits requested in the declaration.
Microsoft Specific
The ordering of data declared as bit fields is from low to high bit, as shown in the figure above.
END Microsoft Specific
If the declaration of a structure includes an unnamed field of length 0, as shown in the following example,
// bit_fields2.cpp
// compile with: /LD
struct Date {
   unsigned nWeekDay  : 3;    // 0..7   (3 bits)
   unsigned nMonthDay : 6;    // 0..31  (6 bits)
   unsigned           : 0;    // Force alignment to next boundary.
   unsigned nMonth    : 5;    // 0..12  (5 bits)
   unsigned nYear     : 8;    // 0..100 (8 bits)
};
the memory layout is as shown in the following figure.
Layout of Date Object with Zero-Length Bit Field

这里写图片描述

The underlying type of a bit field must be an integral type, as described in Fundamental Types.

The code to tell you the reason

#include <stdio.h>

#include <iostream>

// bit_fields1.cpp
// compile with: /LD
struct Date {
   unsigned short nWeekDay  : 3;    // 0..7   (3 bits)
   unsigned short nMonthDay : 6;    // 0..31  (6 bits)
   unsigned short nMonth    : 5;    // 0..12  (5 bits)
   unsigned short nYear     : 8;    // 0..100 (8 bits)
};


// bit_fields2.cpp
// compile with: /LD
struct Date1 {
   unsigned int nWeekDay  : 3;    // 0..7   (3 bits)
   unsigned int nMonthDay : 6;    // 0..31  (6 bits)
   unsigned int          : 0;    // Force alignment to next boundary.
   unsigned int nMonth    : 5;    // 0..12  (5 bits)
   unsigned int nYear     : 8;    // 0..100 (8 bits)
};

int main(int argc, char *argv[])
{
    Date d_small={5,21,3,15};
    Date1 d_big={5,21,3,15};

    printf("sizeof(Date)=%d, sizeof(d_small)=%d\n",sizeof(Date),sizeof(d_small) );
    printf("%d-%d-%d  %d\n",d_small.nYear, d_small.nMonth, d_small.nMonthDay, d_small.nWeekDay );
    printf("sizeof(Date1)=%d, sizeof(d_bi)=%d\n",sizeof(Date1),sizeof(d_big) );
    printf("%d-%d-%d  %d\n",d_big.nYear, d_big.nMonth, d_big.nMonthDay, d_big.nWeekDay );

    d_small.nMonth |=0xffffff; //c++ wwarning: overflow in implicit constant conversion
    d_big.nMonth +=1;

    printf("\n%d-%d-%d  %d\n",d_small.nYear, d_small.nMonth, d_small.nMonthDay, d_small.nWeekDay ); 
    printf("%d-%d-%d  %d\n",d_big.nYear, d_big.nMonth, d_big.nMonthDay, d_big.nWeekDay );
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值