Document: WG14 N1356

_Bool bit-fields

Submitter: Fred Tydeman (USA)
Submission Date: 2009-02-06
Related WG14 documents: DR 262, 335
Subject: _Bool bit-fields

A byte is the smallest non-bit-field object. A byte has CHAR_BIT bits. Therefore, a _Bool object has at least CHAR_BIT bits.

Given the above, some of us believe that the following code is strickly conforming.

#include <limits.h>             /* CHAR_BIT */
int main(void){
  struct bits {
   int        : 0;
   _Bool      : 0;              /* force alignment */
   _Bool bbf1 : 1;              /* holds values 0 and 1 */ 
   _Bool bbf8 : CHAR_BIT;       /* supported ??? */
  } bits;
  return 0;

That is, we believe that implementations are required to support widths in the range of 0 to CHAR_BIT (inclusive) for _Bool bit-fields.

The problem with our reasoning is we forget that 'width' has a technical meaning in C99; which is different than size.

Since _Bool objects (set via assignment) can have only the values 0 and 1, a _Bool object only needs 1 value bit.

A _Bool object must be at least CHAR_BIT bits. However, of those bits, only 1 bit need be a value bit, while the rest are padding bits.

Defect Reports 262 and 335 make it clear that _Bool bit-fields are in terms of widths (and not the number of bits in the object). Since _Bool bit-fields are unsigned, the number of value bits (the precision) is the same as the width (sign + value bits).

At least, gcc 4.3.2, considers _Bool bit-fields larger than 1 bit as being an error that ends translation. Hence, strictly conforming programs must limit _Bool bit-fields to just 0 and 1 bits.

Derek Jones' reading of this issue is:

Implementations cannot support a width greater than CHAR_BIT. Nothing is said about them supporting widths in the range 2..CHAR_BIT.

Joseph S. Myers' reading of this issue is:

The "width" of a type is the number of value and sign bits (, so post-TC2 a _Bool:CHAR_BIT bit-field is valid only if the implementation defines _Bool to have CHAR_BIT value bits. GCC defines it to have one value bit with the other bits being padding bits and undefined behavior if you access a _Bool representation with any of the padding bits having a nonzero value (such representations being trap representations). Thus the width of _Bool is 1 with GCC and the diagnostics are required.

As you know, TC2 changed "the number of bits in an object of the type that is specified if the colon and expression are omitted" to "the width of an object of the type that would be specified were the colon and expression omitted". in paragraph 3.

Requested changes to C1x:

Add a footnote to, paragraph 3:

While the number of bits in a _Bool object is at least CHAR_BIT, the width (number of sign and value bits) of a _Bool may be 1 bit.

Add to, paragraph 9:

A _Bool bit-field has the semantics of a _Bool.