从“long long unsigned int”转换到“long long unsigned int:40”从“0xFFFFFFFFFFFFFFFFFF”更改值到“0xFFFFFFFFFF” [-Werror=溢出]

发布于 2025-01-16 07:55:23 字数 1076 浏览 3 评论 0原文

我有这个示例代码,当我尝试修复 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;
}

如何使用联合来定义任意数量的位并将其分配给任何结构字段?

PS 我不能使用枚举和/或定义联合变量;我必须以这种方式使用宏来适应代码库。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2025-01-23 07:55:23

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文