我想声明一个使用冒号指定大小的位域(我不记得调用语法).我想写这个:
void myFunction()
{
unsigned int thing : 12;
...
}
Run Code Online (Sandbox Code Playgroud)
但是GCC说这是一个语法错误(它认为我正在尝试编写一个嵌套函数).我这样做没有问题:
struct thingStruct
{
unsigned int thing : 4;
};
Run Code Online (Sandbox Code Playgroud)
然后将一个这样的结构放在堆栈上
void myFunction()
{
struct thingStruct thing;
...
}
Run Code Online (Sandbox Code Playgroud)
这让我相信它被语法阻止,而不是语义问题.
那么为什么第一个例子不起作用呢?我错过了什么?
不允许使用其地址访问C中的位字段的原因是什么,是因为它可能不是一个非系统字对齐的地址..?或者因为在一个字节内得到位的地址没有意义......?(因为这种类型的指针运算会很尴尬吗?)
struct node
{
double a : 23;
int b;
}s;
int main()
{
printf("%d\n",sizeof(s));
}
Run Code Online (Sandbox Code Playgroud)
为什么会产生编译错误?我想知道为什么我们不能用双数据类型做位域.
我想要一个1位整数的typedef,所以我虽然这样typedef int:1 FLAG;但是我遇到错误,有没有办法可以这样做?谢谢
我的代码中需要以下两个非常相似的结构:
union ArrayEntry2Byte { union ArrayEntry4Byte {
struct { struct {
char foo : 1; char foo : 1;
short bar : 15; int bar : 31;
}; };
short foobar; int foobar;
// other code // other code
}; };
Run Code Online (Sandbox Code Playgroud)
为了提高此代码的可维护性和优雅性,我想使用模板编程删除其中一个结构:
template<typename value_type>
union ArrayEntry {
struct {
char foo : 1;
value_type bar : 15;
};
value_type foobar;
// other code
};
Run Code Online (Sandbox Code Playgroud)
然而,位域让我陷入困境.我只需要这样的东西:
value_type bar : (value_type == short ? 15 : 31);
Run Code Online (Sandbox Code Playgroud)
但是,嗯,这显然没有正确的语法.我怎样才能优雅地解决这个问题?
假设我的结构定义如下:
typedef struct Counters {
uint8_t counterSec : 6;
uint8_t : 3;
uint8_t counterMin : 6;
uint8_t : 3;
uint8_t counterHr : 5;
uint8_t : 1;
};
Run Code Online (Sandbox Code Playgroud)
由于我希望每个前两个计数器专用6位,每个最后一个计数器为5位,因此我最终得到17位,这意味着总共24位将被分配给计数器类型结构变量的实例.定义空位字段可能是一个实际意义,例如在上面的结构中 - 如果有的话?
针对这个问题的两面性 - 以前确实提出了类似的问题.我遇到空位字段的情况与微控制器编程和配置寄存器有关 - 因此,问题是在该特定情况下使用空位字段.不幸的是,类似帖子的答案并没有解决这些问题.希望这将证明这个问题多一点:)
我正在使用C和bswap_ {16,32,64}来自byteswap.h的宏从big-endian读取/编写一个小端格式的二进制文件,用于字节交换.
除了40位的位字段外,所有值都被正确读取和写入.
该bswap_40宏不存在,我不知道该怎么做,或有没有更好的解决方案是可能的.
这是一个显示此问题的小代码:
#include <stdio.h>
#include <inttypes.h>
#include <byteswap.h>
#define bswap_40(x) bswap_64(x)
struct tIndex {
uint64_t val_64;
uint64_t val_40:40;
} s1 = { 5294967296, 5294967296 };
int main(void)
{
// write swapped values
struct tIndex s2 = { bswap_64(s1.val_64), bswap_40(s1.val_40) };
FILE *fp = fopen("index.bin", "w");
fwrite(&s2, sizeof(s2), 1, fp);
fclose(fp);
// read swapped values
struct tIndex s3;
fp = fopen("index.bin", "r");
fread(&s3, sizeof(s3), 1, fp);
fclose(fp);
s3.val_64 = bswap_64(s3.val_64);
s3.val_40 = …Run Code Online (Sandbox Code Playgroud) 我有这两种结构: - 1.
typedef struct bitfield
{
unsigned int a:16;
unsigned int b:17;
union
{
unsigned int c:4;
unsigned int d:32;
};
}bfield;
Run Code Online (Sandbox Code Playgroud)
这个结构有匿名联合,当我计算这个结构的大小时 - 它出来是12个字节(4 + 4 + 4).这可以.
2.
typedef struct bitfield
{
unsigned int a:16;
unsigned int b:17;
union u
{
unsigned int c:4;
unsigned int d:32;
};
}bfield;
Run Code Online (Sandbox Code Playgroud)
但我在32位机器上的DevC++编译器为这个结构的大小打印了8个字节.我不明白为什么它会出现8.
我正在开发一个在Atmel AT90CAN128上运行的程序.连接到该控制器有40个设备,每个设备都具有状态(开/关).由于我需要通过串行通信向PC报告每个设备的状态,因此我有40位,用于定义设备是打开还是关闭.此外,PC可以打开或关闭任何此设备.
所以,我的第一次尝试是创建以下结构:
typedef struct {
unsigned char length; //!< Data Length
unsigned data_type; //!< Data type
unsigned char data[5]; //!< CAN data array 5 * 8 = 40 bits
} SERIAL_packet;
Run Code Online (Sandbox Code Playgroud)
这个问题是PC会发送一个unsigned char address告诉我设备打开/关闭,所以访问对应于该address数字的位变得相当复杂......
所以我开始寻找选项,我偶然发现了C99 _Bool类型.我想,很好,现在我只是创建一个_Bool data[40],我可以address通过索引我的data数组访问该位.事实证明,在C(或C++)中,内存映射需要一个完整的字节来寻址它.所以,即使我声明_Bool的是,大小_Bool将是8位这是一个问题(它需要尽可能快地让更多的比特我送较慢它获取,而电脑将specting 40位只),而不是非常有效的沟通.所以我开始研究Bit Fields,并尝试了以下方法:
typedef struct {
unsigned char length; //!< Data Length
unsigned data_type; //!< Data type
arrayData data[40]; //!< Data array 5 bytes == 40 …Run Code Online (Sandbox Code Playgroud) 参照在代码片段cppreference.com这我下面粘贴为什么整数b,并c在相同的内存位置分配:
struct S {
char a; // memory location #1
int b : 5; // memory location #2
int c : 11, // memory location #2 (continued)
: 0,
d : 8; // memory location #3
struct {
int ee : 8; // memory location #4
} e;
} obj; // The object 'obj' consists of 4 separate memory locations
Run Code Online (Sandbox Code Playgroud)
我的理解是,例如,在1字节= 8位的系统中,变量a将占用1字节。然后b需要说4个字节。如果同时b并c在相同的内存位置这将填补8个字节去,这是否意味着8个char变量可以在相同的内存位置中持续地分配呢?
另外,程序如何知道要访问的位置b …