定义可访问位,半字节,字节工会 [英] Define union that can access bits, nibbles, bytes
本文介绍了定义可访问位,半字节,字节工会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
union bits {
unsigned int a : 1;
unsigned int b : 2;
unsigned int c : 3;
unsigned int d : 4;``
unsigned char x[2];
unsigned int z;
};
假设在32位的联盟,
我需要使用单个位,或一组位,或半字节或字节。有没有办法来定义的结合。
Suppose in a union of 32 bits, i need to use a single bit, or a group of bits, or nibble, or bytes. is there a way to define the union.
推荐答案
您需要位域的联合。如果只是用一个工会所有的领域将指向同一个地方。
You need a union of bitfields. If you just use a union all of your fields will point to the same place.
union{
struct {
unsigned int bit1 : 1;
unsigned int bit2 : 1;
unsigned int bit3 : 1;
unsigned int bit4 : 1;
unsigned int bit5 : 1;
unsigned int bit6 : 1;
unsigned int bit7 : 1;
unsigned int bit8 : 1;
...
unsigned int bit32 : 1;
};
struct {
unsigned int nibble1 : 4;
unsigned int nibble2 : 4;
...
};
struct {
unsigned int byte1 : 8;
unsigned int byte2 : 8;
...
};
unsigned int int_value;
}
这篇关于定义可访问位,半字节,字节工会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文