English 中文(简体)
是否有办法与工会接触个人。
原标题:Is there a way to access individual bits with a union?

我正在撰写一项C方案。 我想一个变量,我可以作为一个洞穴来查阅,但我也可以获得具体界限。 我认为,我可以采用这样的结合。

typedef union 
{
    unsigned char status;
    bit bits[8];
}DeviceStatus;

but the compiler doesn t like this. Apparently you can t use bits in a structure. So what can I do instead?

最佳回答

当然,但你实际上想用一种障碍来界定这种界限。

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;

然后,你可以查阅<代码>。 装置名称;, 您可访问< 代码>ds.u.bit1。 此外,一些编辑实际上将允许你在工会中拥有匿名结构,因此,你只能查阅<代码>ds.bit1。 如果你从那类f中om出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...

第一个是(至少可以说)更方便的,但当你重新处理地位界限时,机会是该守则甚至稍有可乘之机,因此你可能不关心这一点。

正如已经说过的那样,你可以把记忆放在比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)
}

这使你接近于你提议的系统,该系统将使用[]而不是()。

typedef 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;

可在C区处理最小的单位总是按部就班(C中称为char)。 我们不能直接接触。 取得比额的最近途径是界定一个称为<条码>的轨道点/代码”的数据类别,并为之界定一些功能或宏观:

#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;
}

我建议反对工会,因为它是执行-规定的。 是否填满了刀具或灯具。

你们可以通过在工会内部设置障碍来做到这一点,但是,根据您的履行情况,它可能或不可能发挥作用。 语言定义没有具体规定,按照哪条顺序,分别的比值将与<代码>unsign char/code>的比值相匹配;更糟糕的是,它甚至没有保证借方与<代码>unsign char重叠。 (汇编者可决定将单独的比照放在一个词的最重要方面,而<代码>unsign char/code>向最不重要的方面或反之亦然。)

你们的通常做法是利用双向行动。 界定在轨迹含义后点名的常数,例如

#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 */




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签