如何获得的比特阵列中的结构?

问题描述:

我琢磨(因此正在寻找一种方式来学习这一点,并没有更好的解决办法)是否有可能得到位的数组中的结构。

I was pondering (and therefore am looking for a way to learn this, and not a better solution) if it is possible to get an array of bits in a structure.

让我用一个例子证明。试想一下,这样的code:

Let me demonstrate by an example. Imagine such a code:

#include <stdio.h>

struct A
{
    unsigned int bit0:1;
    unsigned int bit1:1;
    unsigned int bit2:1;
    unsigned int bit3:1;
};

int main()
{
    struct A a = {1, 0, 1, 1};
    printf("%u\n", a.bit0);
    printf("%u\n", a.bit1);
    printf("%u\n", a.bit2);
    printf("%u\n", a.bit3);
    return 0;
}

在此code,我们装在一个struct 4各个位。它们可以单独进行访问,留下位操作的作业到编译器​​。我想知道是,如果这样的事情是可能的:

In this code, we have 4 individual bits packed in a struct. They can be accessed individually, leaving the job of bit manipulation to the compiler. What I was wondering is if such a thing is possible:

#include <stdio.h>

typedef unsigned int bit:1;

struct B
{
    bit bits[4];
};

int main()
{
    struct B b = {{1, 0, 1, 1}};
    for (i = 0; i < 4; ++i)
        printf("%u\n", b.bits[i]);
    return 0;
}

我试着在结构乙声明 无符号整数位[4]: 1 无符号整数位:1 [4] 或类似的东西都无济于事。我最好的猜测是,的typedef unsigned int类型位:1; ,并使用作为类型,但仍然不的工作。

I tried declaring bits in struct B as unsigned int bits[4]:1 or unsigned int bits:1[4] or similar things to no avail. My best guess was to typedef unsigned int bit:1; and use bit as the type, yet still doesn't work.

我的问题是,这样的事可能吗?如果是的话,怎么样?如果不是,为什么不呢? 1位无符号整数是一个有效的类型,那么为什么你不应该能够得到它的一个阵列?

My question is, is such a thing possible? If yes, how? If not, why not? The 1 bit unsigned int is a valid type, so why shouldn't you be able to get an array of it?

同样,我不想为此更换,我只是想知道这样的事情是如何可能的。

Again, I don't want a replacement for this, I am just wondering how such a thing is possible.

P.S。 ,我这个标记为C ++虽然code是用C写的,因为我假设的方法将两种语言存在。如果有一个C ++特定的方式做到这一点(通过使用语言结构,而不是库)我也有兴趣知道的。

P.S. I am tagging this as C++, although the code is written in C, because I assume the method would be existent in both languages. If there is a C++ specific way to do it (by using the language constructs, not the libraries) I would also be interested to know.

更新:我完全知道我能做到位操作自己。我在过去做了一千倍。我不感兴趣,说使用数组/向量,而不是做位操作一个答案。如果这个结构是可以或不可以,不可替代,我只是在想。

更新:答案为急躁(感谢neagoegab):

Update: Answer for the impatient (thanks to neagoegab):

而不是

typedef unsigned int bit:1;

我可以使用

typedef struct
{
    unsigned int value:1;
} bit;

正确使用的#pragma包

不可能 - :一种构建这样的无法的(的这里 - 不可能

NOT POSSIBLE - A construct like that IS NOT possible(here) - NOT POSSIBLE

有人会尝试这样做,但结果将是一位被​​存储在一个字节

One could try to do this, but the result will be that one bit is stored in one byte

#include <cstdint>
#include <iostream>
using namespace std;

#pragma pack(push, 1)
struct Bit
{
    //one bit is stored in one BYTE
    uint8_t a_:1;
};
#pragma pack(pop, 1)
typedef Bit bit;

struct B
{
    bit bits[4];
};

int main()
{
    struct B b = {{0, 0, 1, 1}};
    for (int i = 0; i < 4; ++i)
        cout << b.bits[i] <<endl;

    cout<< sizeof(Bit) << endl;
    cout<< sizeof(B) << endl;

    return 0;
}

输出:

0 //bit[0] value
0 //bit[1] value
1 //bit[2] value
1 //bit[3] value
1 //sizeof(Bit), **one bit is stored in one byte!!!**
4 //sizeof(B), ** 4 bytes, each bit is stored in one BYTE**

为了从这里一个字节访问各个位是一个例子(请注意,该位域布局是实现相关的)

In order to access individual bits from a byte here is an example (Please note that the layout of the bitfields is implementation dependent)

#include <iostream>
#include <cstdint>
using namespace std;

#pragma pack(push, 1)
struct Byte
{
    Byte(uint8_t value):
        _value(value)
    {
    }
    union
    {
    uint8_t _value;
    struct {
        uint8_t _bit0:1;
        uint8_t _bit1:1;
        uint8_t _bit2:1;
        uint8_t _bit3:1;
        uint8_t _bit4:1;
        uint8_t _bit5:1;
        uint8_t _bit6:1;
        uint8_t _bit7:1;
        };
    };
};
#pragma pack(pop, 1)

int main()
{
    Byte myByte(8);
    cout << "Bit 0: " << (int)myByte._bit0 <<endl;
    cout << "Bit 1: " << (int)myByte._bit1 <<endl;
    cout << "Bit 2: " << (int)myByte._bit2 <<endl;
    cout << "Bit 3: " << (int)myByte._bit3 <<endl;
    cout << "Bit 4: " << (int)myByte._bit4 <<endl;
    cout << "Bit 5: " << (int)myByte._bit5 <<endl;
    cout << "Bit 6: " << (int)myByte._bit6 <<endl;
    cout << "Bit 7: " << (int)myByte._bit7 <<endl;

    if(myByte._bit3)
    {
        cout << "Bit 3 is on" << endl;
    }
}