是否有一个gcc选项来修复此位字段相关的C问题?

是否有一个gcc选项来修复此位字段相关的C问题?,c,gcc,bit-fields,C,Gcc,Bit Fields,我的代码如下所示,但是在gcc编译之后,这个例程的输出总是“unmatch” 在这段代码中,RXC_插槽的宽度似乎总是8位,而不是6位。为什么? #include <stdint.h> #include <stdio.h> typedef struct _RX_CONROL_t { union { struct { //32'b{RESERVED12, RX_ABORT, RXC_RECEIVE, RXC_SOURCE_P

我的代码如下所示,但是在gcc编译之后,这个例程的输出总是“unmatch”

在这段代码中,RXC_插槽的宽度似乎总是8位,而不是6位。为什么?

#include <stdint.h>
#include <stdio.h>

typedef struct _RX_CONROL_t {
    union {
        struct {
            //32'b{RESERVED12, RX_ABORT, RXC_RECEIVE, RXC_SOURCE_PORT, RXC_SLOT}
            //32'b{     20bit,     1bit,        1bit,            4bit,     6bit}
             uint8_t RXC_SLOT         : 6;
             uint8_t RXC_SOURCE_PORT  : 4;
             uint8_t RXC_RECEIVE      : 1;
             uint8_t RX_ABORT         : 1;
             uint32_t RESERVED12       : 20;
        };
        uint32_t VALUE32;
    };
} RX_CONROL_t;

int main(void) {

    RX_CONROL_t rx_control = {.VALUE32 = 0};

    rx_control.RXC_SLOT = 3;
    rx_control.RXC_SOURCE_PORT = 2;
    rx_control.RXC_RECEIVE = 1;
    rx_control.RX_ABORT = 0;
    //32'b{RESERVED12, RX_ABORT, RXC_RECEIVE, RXC_SOURCE_PORT, RXC_SLOT}
    //32'b{     20'd0,     1'd0,        1'b1,            4'd2,     6'd3} == 32'h00000483
    if(rx_control.VALUE32 == 0x00000483) {
        printf("match\n");
        return 0;
    }
    else {
        printf("unmatch rx_control.VALUE32 is %x\n", rx_control.VALUE32); //FIXME: rx_control.VALUE32 is 1203 (why?)
        printf("%x %x %x %x %x\n", rx_control.RXC_SLOT, rx_control.RXC_SOURCE_PORT, rx_control.RXC_RECEIVE, rx_control.RX_ABORT, rx_control.RESERVED12);
        return 1;
    }
}
#包括
#包括
类型定义结构控制{
联合{
结构{
//32'b{RESERVED12,RX_中止,RXC_接收,RXC_源_端口,RXC_插槽}
//32'b{20位,1位,1位,4位,6位}
uint8_t RXC_插槽:6个;
uint8_t RXC_源_端口:4;
uint8_t RXC_接收:1;
uint8接收中止:1;
uint32保留12:20;
};
uint32_t值32;
};
}接收控制;
内部主(空){
RX控制RX控制={.VALUE32=0};
rx_control.RXC_插槽=3;
rx_control.RXC_SOURCE_PORT=2;
rx_control.RXC_RECEIVE=1;
rx_control.rx_ABORT=0;
//32'b{RESERVED12,RX_中止,RXC_接收,RXC_源_端口,RXC_插槽}
//32'b{20'd0,1'd0,1'b1,4'd2,6'd3}==32'h00000483
如果(rx_control.VALUE32==0x00000483){
printf(“匹配\n”);
返回0;
}
否则{
printf(“unmatch rx_control.VALUE32是%x\n”,rx_control.VALUE32);//修复:rx_control.VALUE32是1203(为什么?)
printf(“%x%x%x%x%x\n”,rx\u control.RXC\u插槽,rx\u control.RXC\u源端口,rx\u control.RXC\u接收,rx\u control.rx\u中止,rx\u控制保留12);
返回1;
}
}

因为你说它是
uint8\t
。尝试将其设置为
uint32\t
。或者可能使用位掩码而不是位字段。@n.“代词”m.谢谢,但是RXC\u源\u端口、RXC\u接收、RX\u中止具有正确的位宽度。RXC_插槽的不正确位宽度将其他字段推到更高的位置。最初,我认为因为“RXC_插槽+RXC_源_端口”的6+4=10位宽度超过了uint8_t的边界,所以编译器为RXC_源_端口启动了一个新区域。因为
uint32_t
位字段必须与
uint8_t
位字段分开存储,所以前4位字段上有4位填充,在
uint32\t
位字段之前。让它们都
uint32\u t
,它们可以共享同一个存储单元。uint32\u t工作正常,谢谢!!您基本上是对的:6+4位不适合您的
uint8\u t
,因此第二个字段被放入类型
uint8\u t
的下一个元素中。位字段通常不会跨unterling类型的边界存储。但当然,由于它们是实现定义的,如果编译器这样做,我也不会太惊讶。。。