Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Bit-fields

      From cppreference.com
      <c‎ |language
       
       
       
       

      Declares a member with explicit width, in bits. Adjacent bit-field members may be packed to share and straddle the individual bytes.

      A bit-field declaration is astruct orunion member declaration which uses the followingdeclarator:

      identifier (optional):width
      identifier - a name of the bit-field that is being declared. The name is optional: nameless bit-fields introduce the specified number of bits of padding
      width - an integerconstant expression with a value greater or equal to zero and less or equal the number of bits in the underlying type. When greater than zero, this is the number of bits that this bit-field will occupy. The value zero is only allowed for nameless bit-fields and has special meaning: it specifies that the next bit-field in the class definition will begin at an allocation unit's boundary.

      Contents

      [edit]Explanation

      Bit-fields can have only one of the following (possiblyconst orvolatile qualified) types:

      • unsignedint, for unsigned bit-fields (e.g.unsignedint b:3; has the range[07])
      • signedint, for signed bit-fields (signedint b:3; has the range[-43])
      • int, for bit-fields with implementation-defined signedness (note that this differs from the meaning of the keywordint everywhere else, where it means "signed int"). For example,int b:3; may have the range of values[07] or[-43].
      • _Bool, for single-bit bit-fields (e.g.bool x:1;) has the range[01] andimplicit conversions to and from it follow the boolean conversion rules.
      (since C99)
      • bit-precise integer types (e.g._BitInt(5):4; has the range[-87] andunsigned _BitInt(5):4; has the range[015]).
      (since C23)

      Additional implementation-defined types may be acceptable.It is also implementation-defined whether a bit-field may haveatomic type.(since C11)The number of bits in a bit-field (width) sets the limit to the range of values it can hold:

      Run this code
      #include <stdio.h> struct S{// three-bit unsigned field,// allowed values are 0...7unsignedint b:3;}; int main(void){struct S s={7};++s.b;// unsigned overflowprintf("%d\n", s.b);// output: 0}

      Multiple adjacent bit-fields are permitted to be (and usually are) packed together:

      Run this code
      #include <stdio.h> struct S{// will usually occupy 4 bytes:// 5 bits: value of b1// 11 bits: unused// 6 bits: value of b2// 2 bits: value of b3// 8 bits: unusedunsigned b1:5,:11, b2:6, b3:2;}; int main(void){printf("%zu\n",sizeof(struct S));// usually prints 4}

      The specialunnamed bit-field ofwidth zero breaks up padding: it specifies that the next bit-field begins at the beginning of the next allocation unit:

      Run this code
      #include <stdio.h> struct S{// will usually occupy 8 bytes:// 5 bits: value of b1// 27 bits: unused// 6 bits: value of b2// 15 bits: value of b3// 11 bits: unusedunsigned b1:5;unsigned:0;// starts a new unsigned intunsigned b2:6;unsigned b3:15;}; int main(void){printf("%zu\n",sizeof(struct S));// usually prints 8}

      Because bit-fields do not necessarily begin at the beginning of a byte, address of a bit-field cannot be taken. Pointers to bit-fields are not possible. Bit-fields cannot be used withsizeof and_Alignas(since C11)(until C23)alignas(since C23)(since C11).

      [edit]Notes

      The following usages of bit-fields causesundefined behavior:

      The following properties of bit-fields areunspecified:

      • Alignment of the allocation unit that holds a bit-field.

      The following properties of bit-fields areimplementation-defined:

      • Whether bit-fields of typeint are treated as signed or unsigned.
      • Whether types other thanint,signedint,unsignedint,_Bool(since C99), and (possiblyunsigned)_BitInt(N)(since C23) are permitted.
      • Whether atomic types are permitted.
      (since C11)
      • Whether a bit-field can straddle an allocation unit boundary.
      • The order of bit-fields within an allocation unit (on some platforms, bit-fields are packed left-to-right, on others right-to-left).

      Even though the number of bits in the object representation of_Bool is at leastCHAR_BIT, thewidth of the bit-field of type_Bool cannot be greater than1.

      (since C99)

      In the C++ programming language, the width of a bit-field can exceed the width of the underlying type (but the extra bits are padding bits), and bit-fields of typeint are always signed.

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.7.2.1 Structure and union specifiers
      • C17 standard (ISO/IEC 9899:2018):
      • 6.7.2.1 Structure and union specifiers
      • C11 standard (ISO/IEC 9899:2011):
      • 6.7.2.1 Structure and union specifiers
      • C99 standard (ISO/IEC 9899:1999):
      • 6.7.2.1 Structure and union specifiers
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.5.2.1 Structure and union specifiers

      [edit]See also

      C++ documentation forBit-field
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/bit_field&oldid=180012"

      [8]ページ先頭

      ©2009-2025 Movatter.jp