Skip to content

Commit 8c25f7f

Browse files
author
Colin Robertson
authored
Merge pull request #3376 from workingjubilee/patch-2
Document MSVC doesn't straddle bit-fields
2 parents b174a6e + 6649162 commit 8c25f7f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

docs/c-language/c-bit-fields.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ cccccccb bbbbaaaa
7373

7474
Since the 8086 family of processors stores the low byte of integer values before the high byte, the integer `0x01F2` above would be stored in physical memory as `0xF2` followed by `0x01`.
7575

76+
The ISO C99 standard lets an implementation choose whether a bit field may straddle two storage instances. Consider this structure, which stores four bit fields that total 64 bits:
77+
78+
```C
79+
struct
80+
{
81+
unsigned int first : 9;
82+
unsigned int second : 7;
83+
unsigned int may_straddle : 30;
84+
unsigned int last : 18;
85+
} tricky_bits;
86+
```
87+
88+
A standard C implementation could pack these bit fields into two 32-bit integers. It might store `tricky_bits.may_straddle` as 16 bits in one 32-bit integer and 14 bits in the next 32-bit integer. The Windows ABI convention packs bit fields into single storage integers, and doesn't straddle storage units. The Microsoft compiler stores each bit field in the above example so it fits completely in a single 32-bit integer. In this case, `first` and `second` are stored in one integer, `may_straddle` is stored in a second integer, and `last` is stored in a third integer. The `sizeof` operator returns `12` on an instance of `tricky_bits`. For more information, see [Padding and alignment of structure members](padding-and-alignment-of-structure-members.md).
89+
7690
**END Microsoft Specific**
7791

7892
## See also

0 commit comments

Comments
 (0)