C++ Bit Fields

http://msdn.microsoft.com/en-us/library/ewwyfdbe%28v=vs.71%29.aspx

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
struct Date
{
   unsigned nWeekDay  : 3;    // 0..7   (3 bits)
   unsigned nMonthDay : 6;    // 0..31  (6 bits)
   unsigned nMonth    : 5;    // 0..12  (5 bits)
   unsigned nYear     : 8;    // 0..100 (8 bits)
};

int main()
{
}

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 int. Therefore, it is begun at the beginning of a new unsigned int.

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
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)
};

int main()
{
}

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.

C++ Bit Fields,布布扣,bubuko.com

时间: 2024-10-14 07:27:04

C++ Bit Fields的相关文章

Poj 3254 Corn Fields(状态压缩)

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8291   Accepted: 4409 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yumm

BZOJ1725: [Usaco2006 Nov]Corn Fields牧场的安排

1725: [Usaco2006 Nov]Corn Fields牧场的安排 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 400  Solved: 290[Submit][Status] Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧场上的某几格土地里种上美味的草,供他的奶牛们享用.遗憾的是,有些土地相当的贫

POJ 3254 Corn Fields 状态压缩DP (C++/Java)

http://poj.org/problem?id=3254 题目大意: 一个农民有n行m列的地方,每个格子用1代表可以种草地,而0不可以.放牛只能在有草地的,但是相邻的草地不能同时放牛, 问总共有多少种方法. 思路: 状态压缩的DP. 可以用二进制数字来表示放牧情况并判断该状态是否满足条件. 这题的限制条件有两个: 1.草地限制. 2.相邻限制. 对于草地限制,因为输入的时候1是可以种草地的. 以"11110"草地分析,就只有最后一个是不可以种草的.取反后得00001  .(为啥取反

解决mysqldump备份报错: Couldn&#39;t execute &#39;SHOW FIELDS FROM Unknown error 1356

服务器环境: [[email protected] mysql]# cat /etc/redhat-releaseCentOS Linux release 7.3.1611 (Core) [[email protected] mysql]# uname -aLinux localhost.localdomain 3.10.0-514.21.1.el7.x86_64 #1 SMP Thu May 25 17:04:51 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

Consolidate data by using multiple page fields

https://support.office.com/en-us/article/Consolidate-multiple-worksheets-into-one-PivotTable-report-3AE257D2-CA94-49FF-A481-E9FC8ADEEEB5 You can create multiple page fields and assign your own item names for each source range. This lets you create pa

(寒假集训)Watering the Fields (最小生成树)

Watering the Fields 时间限制: 1 Sec  内存限制: 64 MB提交: 26  解决: 10[提交][状态][讨论版] 题目描述 Due to a lack of rain, Farmer John wants to build an irrigation system to send water between his N fields (1 <= N <= 2000). Each field i is described by a distinct point (x

(转)Image Segmentation with Tensorflow using CNNs and Conditional Random Fields

Daniil's blog Machine Learning and Computer Vision artisan. About/ Blog/ Image Segmentation with Tensorflow using CNNs and Conditional Random Fields Tensorflow and TF-Slim | Dec 18, 2016 A post showing how to perform Image Segmentation with a recentl

POJ3254 Corn Fields 状态压缩DP

题目大意是在一块M行N列的农场上种谷物,但是不希望彼此相邻(共用一条边),并且有些地方不能种植谷物,给定M,N(范围都不超过12)以及一些不能种谷物的位置,求出一共有多少种方法种谷物. 状态压缩DP,设dp(i, k) 为种到第i行时,第i行状态为k的总共方案数,可以知道dp(i, k) = ∑dp(i -1, k'),其中我们要判断彼此相邻的情况以及不能种植的情况即可. #include <stdio.h> #include <vector> #include <math.

SSRS Fields cannot be used in page headers or footers

问题环境:SSRS 2005 报表开发 尝试在Page Header中使用Data Set的字段,遇到下面的错误: The value expression for textbox '' refersto a field. Fields cannot be used in page headers or footers. 解决办法: 1.  在Page body页面托入一个text框命名为text1,并且使用data set的字段赋值. 2.  在page header中拖入一个text框,使用

[USACO14MAR]浇地Watering the Fields

https://www.luogu.org/problem/show?pid=2212 题目描述 Due to a lack of rain, Farmer John wants to build an irrigation system to send water between his N fields (1 <= N <= 2000). Each field i is described by a distinct point (xi, yi) in the 2D plane, with