在深入探讨该主题后,我发现如果没有按位运算符和有效的构造函数,多个位字段将毫无用处,这在很大程度上取决于操作系统。
答案在 windows 7 上的 cygwin ( -Wno-unused-variable -O0 -ggdb flags ) 上测试过
版本 1:union
这是没有任何位域的基本实现,最常见的 4 字节颜色空间实现。
#include <iostream>
union c_space32{
uint32_t space;
uint8_t channels[4];
};
int main(){
{ // just a anonymous scope to keep things clear
union c_space32 temp = {0xff00fe32};
std::cout << "sizeof : " << sizeof( union c_space32 ) << "\n\n";
std::cout << (int)temp.channels[1] << "\t" << std::hex << temp.space << "\n";
++temp.channels[1];
std::cout << (int)temp.channels[1] << "\t" << std::hex << temp.space << "\n";
++temp.channels[1];
std::cout << (int)temp.channels[1] << "\t" << std::hex << temp.space << "\n";
}
return 0;}
联合表现为正常颜色空间,联合的每个uint8_t 部分表现为唯一字节,因此c_space32.channels 中值的整体变化不会影响c_space32.space 范围之外的值字节。这是我得到的输出。
sizeof : 4
fe ff00fe32
ff ff00ff32
0 ff000032
版本 2:bit-fields
位域的问题(在某些情况下缺乏文档)是它们的大小很容易改变,并且字节序取决于操作系统,因此位域结构背后的本机逻辑可以逃避我们的人类逻辑。让我给你一些例子,供未来希望努力进入这个话题的人/女孩。
#include <iostream>
#include <bitset>
struct temp1{
uint8_t a:1;
temp1(uint8_t val){ // just so i can assign it
this->a = (val & 0x1 ); // this is needed to avoid truncated warning
}
};
int main(){
struct temp1 t1 = 3;
uint8_t *ptr = (uint8_t *)&t1;
std::cout << sizeof(struct temp1) << std::endl; // size of 1 byte
std::cout << std::bitset<8>( *ptr ) << std::endl; // 0000-0001 position of our bitfield
return 0;}
所以在这种情况下sizeof(struct temp1) 返回大小为 1 byte。我们的位字段的位置为最右上。这里是文档开始走向 MIA 的地方。
#include <iostream>
#include <bitset>
struct temp2{
uint8_t a:1;
uint8_t b:1;
uint8_t c:1;
temp2(int VAL){ // just so i can assign it
this->a = (VAL & 0x1 );
this->b = 0;
this->c = (VAL >> 2 ) & 0x1;
}
};
int main(){
struct temp2 t1 = 0xf;
uint8_t *ptr = (uint8_t *)&t1;
std::cout << sizeof(struct temp2) << std::endl; // size of 1
std::cout << std::bitset<8>( *ptr ) << std::endl; // 0000-0101
return 0;}
在这种情况下constructor 是必须拥有,因为计算机不知道您想如何构造数据。当然,在我们的逻辑中,如果我们将位对齐,那么分配它们与共享内存相同是合乎逻辑的。但问题是计算机不会为我们做bitwise operators。当然这些位是按顺序排列的,但计算机只是抓取一些位并将其定义为一个唯一变量,您选择在该变量中放置什么由您决定。
如果我们超出 unit memory size (byte) 的范围,操作系统就会开始干扰我们的工作。
#include <iostream>
#include <bitset>
struct temp3{
bool b0:1;
bool b1:1;
bool b2:1;
bool b3:1;
bool b4:1;
bool b5:1;
bool b6:1;
bool b7:1;
temp3( int a ){
this->b0 = ( a & 0x1 );
this->b1 = ( a & 0x2 );
this->b2 = ( a & 0x4 );
this->b3 = ( a & 0x8 );
this->b4 = ( a & 0x10 );
this->b5 = ( a & 0x20 );
this->b6 = ( a & 0x40 );
this->b7 = ( a & 0x80 );
}
};
int main(){
struct temp3 t1 = 0xc3;
uint8_t *ptr = (uint8_t *)&t1;
std::cout << sizeof(struct temp3) << std::endl; // still size of 1
std::cout << std::bitset<8>( *ptr ) << std::endl; // 1100-0011
return 0;}
当我们超过字节大小时:
#include <iostream>
#include <bitset>
struct temp4{
bool b0:1;
bool b1:1;
bool b2:1;
bool b3:1;
bool b4:1;
bool b5:1;
bool b6:1;
bool b7:1;
bool b8:1;
temp4( int a ){
this->b0 = ( a & 0x1 );
this->b1 = ( a & 0x2 );
this->b2 = ( a & 0x4 );
this->b3 = ( a & 0x8 );
this->b4 = ( a & 0x10 );
this->b5 = ( a & 0x20 );
this->b6 = ( a & 0x40 );
this->b7 = ( a & 0x80 );
this->b8 = ( a & 0x100 );
}
};
int main(){
struct temp4 t1 = 0x1c3;
uint16_t *ptr = (uint16_t *)&t1;
std::cout << sizeof(struct temp4) << std::endl; // size of 2
std::cout << std::bitset<16>( *ptr ) << std::endl; // 0000-0000 1100-0011
std::cout << t1.b8 << std::endl; // still returns 1
std::cout << "\n\n";
union t_as{
uint16_t space;
temp4 data;
uint8_t bytes[2];
};
union t_as t2 = {0x1c3};
//11000011-00000001
std::cout << std::bitset<8>( t2.bytes[0] ) << "-" << std::bitset<8>( t2.bytes[1] ) << std::endl;
return 0;}
这里发生了什么?由于我们添加了另一个 bool bit-field 我们的结构增长了 1 个字节(因为 bool 是 1 个字节),我们的 16 位指针不显示最后一个 @ 987654339@ - 但工会确实如此。问题是操作系统接管了,在这种情况下,由于先天的操作系统字节序,最后一点停留在我们原始内存后面。正如您在 union 中看到的,字节仍然被读取,但顺序不同。
因此,当超过字节大小时,将应用正常的操作系统规则。
结论和答案
struct half_opacity{
uint8_t alpha:4;
uint8_t red;
uint8_t green;
uint8_t blue;
half_opacity(int a){
this->alpha = ( a >> 24 )&0xf;
this->red = ( a >> 16 )&0xff;
this->green = ( a >> 8 )&0xff;
this->blue = ( a & 0xff );
}
operator uint32_t(){
return ( this->alpha << 24 )
| ( this->red << 16 )
| ( this->green << 8 )
| this->blue;
}
};
{
struct half_opacity c_space = 0xff00AABB;
std::cout << "size of : " << sizeof(struct half_opacity) << std::endl; //size of : 4
std::cout << std::hex << (uint32_t)c_space << std::endl; // 0x0f00AABB
}
因此,除非您打算将原始通道设置为某个位大小,否则我强烈建议您使用union 方法,因为使用bit-fields 将 32 位整数拆分为单个字节并没有任何额外的好处。 bit fields 的主要问题是您需要拆分它们并像任何其他整数字段一样构建然后备份 - bit shifts 通常会规避整个操作系统字节序。
您收到的截断警告是由于您的 struct 中有多个成员,并且 struct 自然分配了第一个成员,并且由于您添加的数量超过了 bit field 的处理能力,编译器警告您一些数据将会丢失。