10

Is there any way to supress the warning generated by gcc in this code:

int main() {
    struct flagstruct {
        unsigned flag : 1;
    } a,b,c;

    a.flag = b.flag | c.flag;

    return a.flag;
}

The warning is

warning: conversion to 'unsigned char:1' from 'int' may alter its value [-Wconversion]

It looks like the the two flags are extended to int when ored together. What I think is really strange is that casting any of the two flags to unsigned supresses the warning.

a.flag = (unsigned)b.flag | c.flag;

Is this a compiler bug or is it supposed to work this way?

4
  • 1
    Please give gcc version.
    – ouah
    Aug 25, 2014 at 8:44
  • 1
    Pick your favorite. They all generate the warning when compiled with -Wconversion
    – MofX
    Aug 25, 2014 at 19:17
  • I would expect warnings about using variables (the 3 single bit definitions a,b,c) that have not been set to a value. Aug 26, 2014 at 4:12
  • @user3629249 true, but not the point
    – MofX
    Aug 26, 2014 at 6:01

3 Answers 3

7

It looks like the the two flags are extended to int when ored together.

This is integer promotion and it is defined in the strangely worded clause 6.3.1.1:2 of the C99 standard:

The following may be used in an expression wherever an int or unsigned int may be used:

— A bit-field of type _Bool, int, signed int, or unsigned int. If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.

First, the processor does not compute directly on bit-fields, and may also not have instructions to compute on the narrower integer types char and short. The C standard captures this by having arithmetic operations defined only on int, unsigned int and wider integer types. Where the standard says “may be used” above, it is trying (poorly) to express that all short types and bit-fields must be promoted to int or unsigned int before participating in arithmetic.

Second, all unsigned bit-fields that are not wide enough to include values that cannot be represented as int are promoted to int. In other words, GCC is behaving according to the standard by promoting your unsigned bit-field into a signed int, and adding an explicit cast, as you did, seems the best policy against bad surprises in the future (and against the warning).

What I think is really strange is that casting any of the two flags to unsigned supresses the warning.

Usual arithmetic conversions, another interesting concept in the C standard (6.3.1.8 in C99), have for consequence that if any of the two operands is explicitly converted to an unsigned int, then the other operand is also, implicitly this time, converted to unsigned int and the | operation is an unsigned int operation producing an unsigned int result.

In other words, (unsigned)b.flag | c.flag is strictly equivalent to (unsigned)b.flag | (unsigned)c.flag. In this case the compiler considers that there is no reason for to warn about the assignment, since the result of the computation is an unsigned int.

2
  • Thanks for your answer. Is still think it is a compiler bug. The standard doesn't tell compiler writes when to emit warnings. So the compiler can know for sure, that no precision is lost in the assignment. The assignemnt 1-bit-bitfield = 32-bit-unsigned; is loosing precision as well and no warning there...
    – MofX
    Aug 25, 2014 at 7:07
  • 2
    I just tried all compilers available on gcc.godbolt.org. Most of them produce a warning, except clang>=3.2. clang-3.0 does produce this warning. So clang seems to agree with me, that it is in fact a compiler bug. The weirdest thing are the messages generated by all avialable gcc/g++ versions: warning: conversion to ‘unsigned char:1’ from ‘unsigned int’ may alter its value [-Wconversion]. Adding the cast to unsigned to the first flag removes the warning. Casting an unsigend to unsigned removes a warning?!
    – MofX
    Aug 25, 2014 at 19:17
4

The best way to solve this warning is to explicitly acknowledge you do not need the excess bits:

a.flag = (b.flag | c.flag) & 0x00000001;

I'm currently using arm-none-eabi-gcc.exe (GNU Tools for ARM Embedded Processors) 5.4.1 20160609 (release) [ARM/embedded-5-branch revision 237715], and this is the only consistent way to get rid of them.

Can't comment on why explicit casting to (unsigned) solves this in the case you posted, though. Peculiar at best, and I doubt whether you will be as lucky under other circumstances, too.

2
  • 1
    @M.M No, I didn't. Seems a bit odd, too. That can only work in case your bitfield is just 1 bit wide. It is not a consistent way to get rid of the warnings arising from using bitfields. Of course, you must mask the value you want to store according to your bitfield size, i.e. & 0x00000003 for 2 bits, etc.
    – parvus
    May 16, 2018 at 17:36
  • 1
    This is ugly and error prone as pravus already stated. If you change the bit-size of your bitfield member, you have to change all masks as well (Surely you could use a macro or a constant, but it is still ugly). The compiler should be smart enough to figure it out on it's own. Just tested GCC >= 5.1: The warning is not shown anymore -> Looks like it really was a compiler bug
    – MofX
    May 18, 2018 at 7:16
2

After one year I revised the issue:

Just tested again with different compiler versions. When I first ran into this bug (now I am quite sure, I am allowed to call it a bug), I already realized the warning existed only in clang < 3.1 and all GCC versions at that time. The warning is still produced by all GCC versions < 5.

Since there is no other way to silence the error, the only solution is to update to GCC > 5 or add the unsigned cast a.flag = (unsigned)b.flag | c.flag;.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.