This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

CARM & bitfields in a structure : is this a bug ?

typedef struct
{
 int query_zone:4;
}statusrequest_t;

statusrequest_t state;

state.query_zone=8;

if ( state.query_zone==8)
 printf("The result matches n=%i n\r",state.query_zone);
else
 printf("This should not occur n=%i\n\r",state.query_zone);

CARM produces : This should not occur n=-8
RV produces : The result matches n=8

typedef struct
{
 short query_zone:4;
}statusrequest_t;


CARM produces : The result matches n=8
RV produces : This should not occur n=-8

typedef struct
{
 signed char query_zone:4;
}statusrequest_t;


CARM produces : The result matches n=8
RV produces : This should not occur n=-8

Why does the comparision fail when query_zone is an int bitfield ?
The RV compiler produces not the same result as CARM

  • Assuming a twos-complement representation, a 4-bit (implicitly signed) integer can hold values in the range -8 through 7. 8 is not in that range.

  • The Standard states that it is implementation-defined whether a bitfield declared without signed or unsigned qualifiers is interpreted as signed or unsigned. When used in expressions, the bit-field value is promoted to the int arithmetic type either signed or unsigned.

    The RV 3.0 Compiler & Libraries MANUAL documents the bit-field allocation rules, and states that unqualified bit-fields are interpreted as unsigned.

    This is why bitfield declarations should always be explicitly sign-qualified, to stay away of compiler implementation choices.

    Your first and last examples for RV are consistent with the manual: the first one is 8, and the last is -8 (binary 1000 sign-extended to 0xFFFFFFF8 in signed 2's complement).

    But your second example is not right. A short bitfield should evaluate to 8, like the first example. I couldn't reproduce your declared behaviour. Are you sure RV is returning -8 for the short bitfield example?

  • Noel, are you still following this thread anymore? Can you give feedback?

  • i will look again at the problem, i am not in the possibility to do this in the next coming days.