我可以引用 C 位域的另一部分吗?
Can I have a bit that references another part of a C bitfield?
我正在尝试编写一个代码片段,其中结构的 属性 引用同一结构的另一个 属性 中的特定位。这看起来像:
struct A {
unsigned char type;
unsigned char is_family_a : 1; // should reference bit 7 of above somehow
};
struct A example;
example.type = 0x17;
printf("%i\n", example.is_family_a); // 0
example.type = 0xF7;
printf("%i\n", example.is_family_a); // 1
我查看了它的 cppreference 页面,但没有看到任何内容。我也查看了 Whosebug,但没有真正找到任何东西。如果我使用宏,这似乎确实有效,但我认为编译器可能比我能更好地优化这类事情。
应该这样做:
struct A {
union {
unsigned char type;
struct {
unsigned char : 7; // remove for big endian
unsigned char is_family_a : 1; // should reference bit 7 of above somehow
};
};
};
我正在尝试编写一个代码片段,其中结构的 属性 引用同一结构的另一个 属性 中的特定位。这看起来像:
struct A {
unsigned char type;
unsigned char is_family_a : 1; // should reference bit 7 of above somehow
};
struct A example;
example.type = 0x17;
printf("%i\n", example.is_family_a); // 0
example.type = 0xF7;
printf("%i\n", example.is_family_a); // 1
我查看了它的 cppreference 页面,但没有看到任何内容。我也查看了 Whosebug,但没有真正找到任何东西。如果我使用宏,这似乎确实有效,但我认为编译器可能比我能更好地优化这类事情。
应该这样做:
struct A {
union {
unsigned char type;
struct {
unsigned char : 7; // remove for big endian
unsigned char is_family_a : 1; // should reference bit 7 of above somehow
};
};
};