[previous] 3661 [next] Plain int bit-field compared with zero.
Arrays, structures, unions and bit-fields REFERENCE - ISO:C90-6.5.2.1 Structure and Union Specifiers - Semantics

The value of a bit-field is being compared with zero. The bit-field has been defined as having type int and so, according the ISO:C standard, it may be either a signed type or an unsigned type; the behaviour is implementation-defined. If it is unsigned, this comparison will effectively be an invariant expression.

By default QA·C will assume that a plain int bit-field is implemented as an unsigned type; but if the -bits+ option is used, it will be configured as a signed type.

Confusion can be avoided by ensuring that bit-fields are always defined explicitly as either signed or unsigned. Message 0634 will identify bit-fields which violate this principle.

For example:


/*PRQA S 2017,2213,3120,3131,3227,3408,3447,3621 ++*/

/****************************************************
 * By default, QAC assumes that a plain int bit-field
 * is implemented as an unsigned type.
 ***************************************************/

struct ST                               /* Message 0634 */
{
    int a:3;
    int b:5;
};

extern struct ST stx;

extern void foo(void)
{
    if (stx.a <  0) { }                  /* Message 3661 and 3316 */
    if (stx.a <= 0) { }                  /*                       */
    if (stx.a == 0) { }                  /*                       */
    if (stx.a >= 0) { }                  /* Message          3324 */
    if (stx.a >  0) { }                  /*                       */
    if (stx.a != 0) { }                  /*                       */

    if (stx.a <  -5) { }                 /* Message 3662 and 3328 */
    if (stx.a <= -5) { }                 /* Message 3662 and 3328 */
    if (stx.a == -5) { }                 /* Message 3662 and 3328 */
    if (stx.a >= -5) { }                 /* Message 3662 and 3328 */
    if (stx.a >  -5) { }                 /* Message 3662 and 3328 */
    if (stx.a != -5) { }                 /* Message 3662 and 3328 */
}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents