是否有gcc选项可解决与位域相关的C问题?

问题描述

我的代码如下所示,但是在gcc编译后,此例程的输出始终为'unmatch'。

在此代码中,RXC_SLOT似乎总是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,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,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;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)