C90: Bitfield as essential boolean type

Moderators: misra-c, david ward

Post Reply
Posts: 1
Joined: Tue Aug 20, 2013 10:13 am
Company: Caterpillar Global Mining

C90: Bitfield as essential boolean type

Post by nunterberg » Tue Mar 03, 2015 4:00 pm

Is it possible to declare bitfields that can be used as essentially boolean expressions in C90 without vialoating any MISRA rules?
Acording to section D.4, only a bit-field that has been implemented with an essential boolean type is essentially boolean. In C90, such a type does not exist as a "native" type. Sections 8.10.2 and D.6 allow an enumerated type as a way to define an essentially Boolean type. However, using such an enumerated type in a bitfield violates Rule 6.1.

Code: Select all

typedef enum { false, true } bool_t;
struct {
    bool_t   flag1:1;  /* violates Rule 6.1 */
    uint16_t flag2:1;  /* compliant */
} bf;

if (bf.flag1) {        /* compliant? */
if (bf.flag2) {        /* violates Rule 14.4? */
Is there a compliant way out of this conflict or do we need to file a deviation for this use?

Posts: 572
Joined: Thu Jan 05, 2006 1:11 pm

Re: C90: Bitfield as essential boolean type

Post by misra-c » Fri Mar 13, 2015 8:57 am

You would need to deviate from rule 6.1 if you wish to use an enumeration type as your essentially boolean type.

An alternative would be to define your bool_t as an unsigned int.

Code: Select all

typedef unsigned int bool_t;
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.10 The essential type model”