我只学习了一个星期的 C++(和编程),所以这个问题可能缺乏对基本编程原理的理解,但这里什么都没有:
unsigned int bitFlags()
{
unsigned char option1 = 0x01; // hex for 0000 0001
unsigned char option2 = 0x02; // hex for 0000 0010
unsigned char option3 = 0x04; // hex for 0000 0100
unsigned char option4 = 0x08; // hex for 0000 1000
unsigned char option5 = 0x10; // hex for 0001 0000
unsigned char option6 = 0x20; // hex for 0010 0000
unsigned char option7 = 0x40; // hex for 0100 0000
unsigned char option8 = 0x80; // hex for 1000 0000
unsigned char myflags; // byte-size value to hold some combination of the above 8 options
myflags |= option1|option2|option3;
if (myflags&option8)
return 1;
else
return 0;
}
int main()
{
std::cout << bitFlags() << "\n";
return 0;
}
所以,我只设置了 3 个标志(选项 1、选项 2、选项 3)。现在,标志查询按预期工作(选项 1/2/3 返回 1,其余返回 0)直到选项 7/8。即使未设置 option7/8,该函数也会返回 1。这让我得出结论,unsigned char myflags 在二进制中看起来像这样:1100 0000。那么,
1)这里发生了什么?为什么 2 位已经在使用?首先如何使用 2 位的无符号字符?不应该只为有符号变量保留“最高”位吗?
2) 为什么我们使用按位赋值运算符 |= 来设置位标志,当它提供意外结果时。如果我们简单地分配 myflags = option3 | 选项2 | option3 它按预期工作 - option7/8 的查询返回 0。
(我很可能不知道我在说什么!)