C++ 重叠位?你的意思是不是struct Bit{union{char\u value;struct{char\u bit0:1;char\u bit1:1;…}\u bits;}?你能解释一下那些布拉格包装吗?它们是标准C还是特定于Visual C?pragm
C++ 重叠位?你的意思是不是struct Bit{union{char\u value;struct{char\u bit0:1;char\u bit1:1;…}\u bits;}?你能解释一下那些布拉格包装吗?它们是标准C还是特定于Visual C?pragm,c++,c,internals,C++,C,Internals,重叠位?你的意思是不是struct Bit{union{char\u value;struct{char\u bit0:1;char\u bit1:1;…}\u bits;}?你能解释一下那些布拉格包装吗?它们是标准C还是特定于Visual C?pragma-pack是实现定义的,因为它不是pragma-STDC(ISO 9899:1999(s)6.10.6.1)。据我所知,它是为MSVC和GCC定义的。请查看关于pragma pack功能的更详细解释。@Shahbaz,是的,应该是这样说的,为
重叠位?你的意思是不是
struct Bit{union{char\u value;struct{char\u bit0:1;char\u bit1:1;…}\u bits;}代码>?你能解释一下那些布拉格包装吗?它们是标准C还是特定于Visual C?pragma-pack
是实现定义的,因为它不是pragma-STDC
(ISO 9899:1999(s)6.10.6.1)。据我所知,它是为MSVC和GCC定义的。请查看关于pragma pack
功能的更详细解释。@Shahbaz,是的,应该是这样说的,为更新太晚而道歉,谢谢。你真的尝试过这个吗?首先,sizeof(struct-bitfield)
本身被舍入为int-size(我用gcc尝试了这个),所以即使这样做有效,您的malloc
也会分配太多内存。相反,您需要类似于malloc(比特数/CHAR比特数)代码>。但是,填充(同样,用gcc尝试)使每个元素都有自己的无符号int
,因此如果从bit
的声明中删除:1
,则您的位将具有相同的地址,因此实际上不会得到位字段。我尝试了这样的代码:(int i=0;i
然后打印查看内存实际位模式的位:printf(“%08X%08X%08X%08X%08X\n”,((uint32_t*)bf)[0],((uint32_*)bf)[1],((uint32_*)bf)[2],((uint32_*)bf)[3])
结果是00000000000001 00000000000001
谢谢!我已经尝试过了,我应该提到它对于节省空间没有用处,因为每个结构的指针大小将是下一个可用地址。但是,如果您正在寻找循环效果(1+1再次变为零),它很有用。我将编辑我的帖子来提及内存问题。如果这不能节省内存,那有什么意义呢?在这种情况下,我们可以使用bool[]
。
#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;
}
#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;
}
typedef unsigned int bit:1;
typedef struct
{
unsigned int value:1;
} bit;
struct Bits {
Word word[];
size_t word_count;
};
#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**
#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;
}
}
struct __attribute__ ((__packed__)) A
{
unsigned int bit0:1;
unsigned int bit1:1;
unsigned int bit2:1;
unsigned int bit3:1;
};
union U
{
struct A structVal;
int intVal;
};
int main()
{
struct A a = {1, 0, 1, 1};
union U u;
u.structVal = a;
for (int i =0 ; i<4; i++)
{
int mask = 1 << i;
printf("%d\n", (u.intVal & mask) >> i);
}
return 0;
}
struct bitfield{
unsigned int bit : 1;
};
struct bitfield *bitstream;
bitstream=malloc( sizeof(struct bitfield) * numberofbitswewant );
bitstream[bitpointer].bit=...