Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.BITFIELD.TYPE

Type of bit-field is not signed/unsigned integer.

MISRA C 2012 Rule 6.1: Bit-fields shall only be declared with an appropriate type

C90 [Undefined 38; Implementation 29], C99 [Implementation J.3.9(1, 2)]

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Amplification

The appropriate bit-field types are:

  • C90: either unsigned int or signed int;
  • C99: one of :
    • either unsigned int or signed int;
    • another explicitly signed or explicitly unsigned integer type that is permitted by the implementation;
    • _Bool.

Note: It is permitted to use typedefs to designate an appropriate type.

Rationale

Using int is implementation-defined because bit-fields of type int can be either signed or unsigned.

The use of enum, short, char or any other type for bit-fields is not permitted in C90 because the behaviour is undefined.

In C99, the implementation may define other integer types that are permitted in bit-field declarations.

Example

The following example is applicable to C90 and to C99 implementations that do not provide any additional bit-field types. It assumes that the int type is 16-bit.

typedef unsigned int UINT_16;

struct s {
   unsigned int b1:2;    /* Compliant                               */ 
   int b2:2;             /* Non-compliant - plain int not permitted */
   UINT_16 b3:2;         /* Compliant - typedef designating unsigned int */ 
   signed long b4:2;     /* Non-compliant even if long and int are the
                          * same size */ 
};

Type of bit-field is not signed/unsigned integer.

MISRA-C 2004 Rule 2.1 (required): Bit fields shall only be defined to be of type unsigned int or signed int.

[Undefined 38; Implementation 29]

Using int is implementation defined because bit fields of type int can be either signed or unsigned. The use of enum, short or char types for bit fields is not allowed because the behaviour is undefined.