Home > Back-end >  conversion from 'long long unsigned int' to 'long long unsigned int:40' changes
conversion from 'long long unsigned int' to 'long long unsigned int:40' changes

Time:03-23

I have this example code that throws an error when I try to fix one of the GCC warnings

#include <stdint.h>

//
typedef union someStruct
{
   uint64_t All;
   struct
   {
      uint64_t Foo  : 40;
      uint64_t Bar  : 24;
   } Field;
} someStruct;

#define bits_64 ((uint64_t)(-1))

//
typedef union bits
{
   uint64_t oneBit: 1;
   uint64_t twoBits: 2;
   uint64_t threeBits: 3;
   uint64_t fourBits: 4;
   uint64_t fiveBits: 5;
   uint64_t sixBits: 6;
   uint64_t sevenBits: 7;
   uint64_t fourtyBits: 40;
   uint64_t All;
} bits;

#define bits_40 (((bits)(-1)).fourtyBits)

//
int main()
{
    someStruct x;
    someStruct y;

    x.Field.Foo = bits_64; //-Woverflow warning

    //trying to fix the warning with using the bits union
    y.Field.Foo = bits_40; // but this throws the error msg below

    /*
        <source>:30:19: error: cast to union type from type not present in union
        30 | #define bits_40 (((bits)(-1)).fourtyBits)
           |                   ^
    */

    return 0;
}

How can I use a union to define any number of bits and assign it to any struct field?

P.S. I cannot use enums and/or define a union variable; I have to use macros this way to fit the codebase.

CodePudding user response:

Your #define for bits_40 should look like this:

#define bits_40 (((bits){.All = -1)).fourtyBits)

You could also just do:

#define bits_40 ((1ULL << 40) - 1)

and skip the bits struct entirely. Or you could define a BIT_MASK macro as follows:

#define BIT_MASK(bits) ((1uLL << bits) - 1)
    :
    :
x.Field.Foo = BIT_MASK(40);
  • Related