Re: Bools and bitfields

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Paulo J. Matos" <pocmatos@xxxxxxxxx> writes:

> Regarding the size of possible bitfields I would like to know why:
> #include <stdbool.h>
> struct s {
>     bool f : 2;
> };
>
> fails to compile in gcc 4.3 and gcc 4.4 but not in gcc 4.2.
>
> sizeof(bool) is 1, and C99 6.7.2.1 paragraph 3 seems to imply that the
> bitfield size shouldn't be bigger than the width of the object.
> "The expression that specifies the width of a bit-field shall be an
> integer constant
> expression with a nonnegative value that does not exceed the width of
> an object of the
> type that would be specified were the colon and expression omitted."
>
> Therefore I would expect to have 1 to 8 as possible values for an 8
> bit/byte architecture.

sizeof(bool) == 1 but the type is nevertheless just 1 bit wide.  The
only valid values to store in a bool are 0 and 1.  It doesn't make
sense to have a bool field that is 2 bits wide, as you can still only
store 0 and 1 in it.

Ian

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux