任何人都可以告诉是否有可能将一种类型的位域分配给另一种类型的位域吗?带有C90编译器的支持。
这是位域的结构:
typedef struct{
unsigned int ERA:2;
unsigned int destar:2;
unsigned int srcar:2;
unsigned int opcode:4;
unsigned int grp:2;
unsigned int dumy:3;
} Command;
typedef struct{
unsigned int cell:15;
} Word;
这是主程序:
int main(int argc, const char * argv[]){
Word word;
Command cmd;
cmd.grp = 2;
word = cmd;
}
假设我在这里有这15位:
我们按以下顺序排列它们:
命令:
Dumy | grp | opcode | srcar | destar | ERA
0 0 0 0 1 0 0 0 1 10 0 0 0 0
单词:
| word |
000000000000000
目的是使单词等于整个Command(word = command
),因此我们将看起来像这样:
| word |
000010001100000
您可能想要的是一个工会:
typedef union {
struct {
//unsigned int:1; // padding bit either here...
unsigned int ERA:2;
unsigned int destar:2;
unsigned int srcar:2;
unsigned int opcode:4;
unsigned int grp:2;
unsigned int dumy:3;
//unsigned int:1; // ...or here, depending on how you want to read it
} cmd;
uint16_t word;
} Cmd;
int main(int argc, const char * argv[]){
Cmd cmd;
cmd.cmd.grp = 2;
printf("word=%u\n", cmd.word);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句