我正在编写一个C程序。我想要一个变量,既可以像char一样访问,又可以访问特定的位。我在考虑可以使用联合体来实现这个需求...
typedef union
{
unsigned char status;
bit bits[8];
}DeviceStatus;
但编译器不喜欢这样。显然在结构体中不能使用位(bit)。那我该怎么办呢?
当然可以,但你实际上想要使用结构体来定义位(bit):
typedef union
{
struct
{
unsigned char bit1 : 1;
unsigned char bit2 : 1;
unsigned char bit3 : 1;
unsigned char bit4 : 1;
unsigned char bit5 : 1;
unsigned char bit6 : 1;
unsigned char bit7 : 1;
unsigned char bit8 : 1;
}u;
unsigned char status;
}DeviceStatus;
然后你可以访问 DeviceStatus ds;
,你可以访问 ds.u.bit1
。另外,一些编译器实际上允许您在联合体内拥有匿名结构,这样您可以只访问 ds.bit1
如果您省略了类型定义中的“u”。
您有几种可能性。其中之一是仅使用布尔数学来获取位:
int bit0 = 1;
int bit1 = 2;
int bit2 = 4;
int bit3 = 8;
int bit4 = 16;
int bit5 = 32;
int bit6 = 64;
int bit7 = 128;
if (status & bit1)
// whatever...
另一种方法是使用位域:
struct bits {
unsigned bit0 : 1;
unsigned bit1 : 1;
unsigned bit2 : 1;
// ...
};
typedef union {
unsigned char status;
struct bits bits;
} status_byte;
some_status_byte.status = whatever;
if (status_byte.bits.bit2)
// whatever...
sizeof(char) < sizeof(DeviceStatus)
,可能会引起一些问题。 - torakunion
中,各个bitX
字段可能会按照编译器的要求进行填充。 - Jens Gustedtunsigned
的大小是无关紧要的,因为该类型用于位域;位域必须声明为 int
(可选带符号)(或在 C99 中使用 _Bool
)。但编译器可能会决定 struct bits
占用多个字节,实际上,使用 gcc,sizeof(struct bits)
= sizeof(int)
。 - Gilles 'SO- stop being evil': 1
,这意味着该值仅使用一个位来存储。它将类型限制为该位数,只要您不使用大于所使用类型大小的数字,就可以正常使用。 - Jon Hannatypedef union
{
unsigned char status;
struct bitFields
{
_Bool bit0 : 1;
_Bool bit1 : 1;
_Bool bit2 : 1;
_Bool bit3 : 1;
_Bool bit4 : 1;
_Bool bit5 : 1;
_Bool bit6 : 1;
_Bool bit7 : 1;
} bits;
}DeviceStatus;
char
一样宽。但这不是重点,因为这些是位域。编译器实际上可以将字段 bitX
放置在状态位上,但没有保证。 - Jens Gustedt正如已经提到的,C语言不能处理比一个字节更小的内存。我会写一个宏:
#define BIT(n) (1 << n)
使用它来访问位。这样,您的访问是相同的,无论您访问的结构的大小如何。您可以编写以下代码:
if (status & BIT(1)) {
// Do something if bit 1 is set
} elseif (~status | BIT(2) {
// Do something else if bit 2 is cleared
} else {
// Set bits 1 and 2
status |= BIT(1) | BIT(2)
// Clear bits 0 and 4
status &= ~(BIT(0) | BIT(4))
// Toggle bit 5
status ^= BIT(5)
}
char
)。您无法直接访问位。访问位的最接近方法是定义一个名为bitpointer
的数据类型,并为其定义一些函数或宏:#include <stdbool.h>
typedef struct bitpointer {
unsigned char *pb; /* pointer to the byte */
unsigned int bit; /* bit number inside the byte */
} bitpointer;
static inline bool bitpointer_isset(const bitpointer *bp) {
return (bp->pb & (1 << bp->bit)) != 0;
}
static inline void bitpointer_set(const bitpointer *bp, bool value) {
unsigned char shifted = (value ? 1 : 0) << bp->bit;
unsigned char cleared = *bp->pb &~ (1 << bp->bit);
*(bp->pb) = cleared | shifted;
}
我建议不要使用联合体,因为它们的填充顺序是实现定义的,可能是从最高有效位到最低有效位或者从最低有效位到最高有效位(参见ISO C99,6.7.2.1p10)。
unsigned char
的位匹配的顺序;更糟糕的是,它甚至不能保证位将与unsigned char
重叠(编译器可能决定将单独的位放置在字的最高有效位或最低有效位的一侧,而将unsigned char
放置在另一侧)。#define FLAG_BUSY 0x01
#define FLAG_DATA_AVAILABLE 0x02
#define FLAG_TRANSMISSION_IN_PROGRESS 0x04
...
#define FLAG_ERROR 0x80
然后是读写单独的位:
if (status & FLAG_BUSY) ... /* test if the device is busy */
status &= ~FLAG_ERROR; /* turn off error flag */
status |= FLAG_TRANSMISSION_IN_PROGRESS /* turn on transmission-in-progress flag */
char
,因此可能根本不会发生别名。 此外,联合内的匿名结构是标准要求的。 - Potatoswatter