从 'long long unsigned int' 到 'long long unsigned int:40' 的转换将值从 '0xFFFFFFFFFFFFFFFF' 更改为 '0xFFFFFFFFFF' [-Werror=overflow]
conversion from 'long long unsigned int' to 'long long unsigned int:40' changes value from '0xFFFFFFFFFFFFFFFF' to '0xFFFFFFFFFF' [-Werror=overflow]
我有这个示例代码,当我尝试修复其中一个 GCC 警告时抛出错误
#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;
}
如何使用联合来定义任意数量的位并将其分配给任意结构字段?
P.S。我不能使用枚举 and/or 定义联合变量;我必须以这种方式使用宏来适应代码库。
bits_40
的 #define
应如下所示:
#define bits_40 (((bits){.All = -1)).fourtyBits)
你也可以这样做:
#define bits_40 ((1ULL << 40) - 1)
并完全跳过 bits
结构。或者您可以定义一个 BIT_MASK
宏,如下所示:
#define BIT_MASK(bits) ((1uLL << bits) - 1)
:
:
x.Field.Foo = BIT_MASK(40);
我有这个示例代码,当我尝试修复其中一个 GCC 警告时抛出错误
#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;
}
如何使用联合来定义任意数量的位并将其分配给任意结构字段?
P.S。我不能使用枚举 and/or 定义联合变量;我必须以这种方式使用宏来适应代码库。
bits_40
的 #define
应如下所示:
#define bits_40 (((bits){.All = -1)).fourtyBits)
你也可以这样做:
#define bits_40 ((1ULL << 40) - 1)
并完全跳过 bits
结构。或者您可以定义一个 BIT_MASK
宏,如下所示:
#define BIT_MASK(bits) ((1uLL << bits) - 1)
:
:
x.Field.Foo = BIT_MASK(40);