You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp/cpp-bit-fields.md
+73-70Lines changed: 73 additions & 70 deletions
Original file line number
Diff line number
Diff line change
@@ -12,74 +12,77 @@ ms.author: "mblome"
12
12
ms.workload: ["cplusplus"]
13
13
---
14
14
# C++ Bit Fields
15
-
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-declarator* specification follows:
16
-
17
-
## Syntax
18
-
19
-
```
20
-
21
-
declarator : constant-expression
22
-
```
23
-
24
-
## Remarks
25
-
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-expression* specifies 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.
26
-
15
+
16
+
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-declarator* specification follows:
17
+
18
+
## Syntax
19
+
20
+
*declarator***:***constant-expression*
21
+
22
+
## Remarks
23
+
24
+
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-expression* specifies 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.
25
+
27
26
> [!NOTE]
28
-
> 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.
29
-
30
-
The following example declares a structure that contains bit fields:
31
-
32
-
```
33
-
// bit_fields1.cpp
34
-
// compile with: /LD
35
-
struct Date {
36
-
unsigned short nWeekDay : 3; // 0..7 (3 bits)
37
-
unsigned short nMonthDay : 6; // 0..31 (6 bits)
38
-
unsigned short nMonth : 5; // 0..12 (5 bits)
39
-
unsigned short nYear : 8; // 0..100 (8 bits)
40
-
};
41
-
```
42
-
43
-
The conceptual memory layout of an object of type `Date` is shown in the following figure.
44
-
45
-

46
-
Memory Layout of Date Object
47
-
48
-
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.
49
-
50
-
**Microsoft Specific**
51
-
52
-
The ordering of data declared as bit fields is from low to high bit, as shown in the figure above.
53
-
54
-
**END Microsoft Specific**
55
-
56
-
If the declaration of a structure includes an unnamed field of length 0, as shown in the following example,
57
-
58
-
```
59
-
// bit_fields2.cpp
60
-
// compile with: /LD
61
-
struct Date {
62
-
unsigned nWeekDay : 3; // 0..7 (3 bits)
63
-
unsigned nMonthDay : 6; // 0..31 (6 bits)
64
-
unsigned : 0; // Force alignment to next boundary.
65
-
unsigned nMonth : 5; // 0..12 (5 bits)
66
-
unsigned nYear : 8; // 0..100 (8 bits)
67
-
};
68
-
```
69
-
70
-
the memory layout is as shown in the following figure.
71
-
72
-

73
-
Layout of Date Object with Zero-Length Bit Field
74
-
75
-
The underlying type of a bit field must be an integral type, as described in [Fundamental Types](../cpp/fundamental-types-cpp.md).
76
-
77
-
## Restrictions on bit fields
78
-
The following list details erroneous operations on bit fields:
79
-
80
-
1. Taking the address of a bit field.
81
-
82
-
2. Initializing a reference with a bit field.
83
-
84
-
## See Also
85
-
[Classes and Structs](../cpp/classes-and-structs-cpp.md)
27
+
> 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.
28
+
29
+
The following example declares a structure that contains bit fields:
30
+
31
+
```cpp
32
+
// bit_fields1.cpp
33
+
// compile with: /LD
34
+
structDate {
35
+
unsigned short nWeekDay : 3; // 0..7 (3 bits)
36
+
unsigned short nMonthDay : 6; // 0..31 (6 bits)
37
+
unsigned short nMonth : 5; // 0..12 (5 bits)
38
+
unsigned short nYear : 8; // 0..100 (8 bits)
39
+
};
40
+
```
41
+
42
+
The conceptual memory layout of an object of type `Date` is shown in the following figure.
43
+
44
+

45
+
Memory Layout of Date Object
46
+
47
+
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.
48
+
49
+
**Microsoft Specific**
50
+
51
+
The ordering of data declared as bit fields is from low to high bit, as shown in the figure above.
52
+
53
+
**END Microsoft Specific**
54
+
55
+
If the declaration of a structure includes an unnamed field of length 0, as shown in the following example,
56
+
57
+
```cpp
58
+
// bit_fields2.cpp
59
+
// compile with: /LD
60
+
struct Date {
61
+
unsigned nWeekDay : 3; // 0..7 (3 bits)
62
+
unsigned nMonthDay : 6; // 0..31 (6 bits)
63
+
unsigned : 0; // Force alignment to next boundary.
64
+
unsigned nMonth : 5; // 0..12 (5 bits)
65
+
unsigned nYear : 8; // 0..100 (8 bits)
66
+
};
67
+
```
68
+
69
+
then the memory layout is as shown in the following figure:
70
+
71
+

72
+
Layout of Date Object with Zero-Length Bit Field
73
+
74
+
The underlying type of a bit field must be an integral type, as described in [Fundamental Types](../cpp/fundamental-types-cpp.md).
75
+
76
+
If the initializer for a reference of type `const T&` is an lvalue that refers to a bit field of type `T`, the reference is not bound to the bit field directly. Instead, the reference is bound to a temporary initialized to hold the value of the bit field.
77
+
78
+
## Restrictions on bit fields
79
+
80
+
The following list details erroneous operations on bit fields:
81
+
82
+
- Taking the address of a bit field.
83
+
84
+
- Initializing a non-**const** reference with a bit field.
85
+
86
+
## See also
87
+
88
+
-[Classes and Structs](../cpp/classes-and-structs-cpp.md)
0 commit comments