为什么的std :: bitset的&LT; 8'变量无法处理11111111? [英] Why is the std::bitset<8> variable unable to handle 11111111?
问题描述
这是为什么程序显示以下输出?
Why is this program showing the following output ?
#include <bitset>
...
{
std::bitset<8> b1(01100100); std::cout<<b1<<std::endl;
std::bitset<8> b2(11111111); std::cout<<b2<<std::endl; //see, this variable
//has been assigned
//the value 11111111
//whereas, during
//execution, it takes
//the value 11000111
std::cout << "b1 & b2: " << (b1 & b2) << '\n';
std::cout << "b1 | b2: " << (b1 | b2) << '\n';
std::cout << "b1 ^ b2: " << (b1 ^ b2) << '\n';
}
这是输出:
01000000
11000111
b1 & b2: 01000000
b1 | b2: 11000111
b1 ^ b2: 10000111
首先,我觉得有什么不对的头文件(我是用MinGW的),所以我
使用MSVCC检查。但它也显示出同样的事情。请帮助。
First, I thought there is something wrong with the header file (I was using MinGW) so I checked using MSVCC. But it too showed the same thing. Please help.
推荐答案
尽管外观上, 11111111
是小数。的 11111111
<子> 10 是 101010011000101011000111
2 <二进制重新presentation / SUB>。在建设中,和std :: bitset&LT; 8&GT;
注意到这一八个最显著位: 11000111
<子> 2 。
Despite the appearance, the 11111111
is decimal. The binary representation of 11111111
10 is 101010011000101011000111
2. Upon construction, std::bitset<8>
takes the eight least significant bits of that: 11000111
2.
第一种情况是,除了 01100100
是八进制(由于前导零)相似。二进制pssed相同数量的前$ P $是 1001000000001000000
2 。
The first case is similar except the 01100100
is octal (due to the leading zero). The same number expressed in binary is 1001000000001000000
2.
重新present一个bitset与 11111111
2 是和std :: bitset℃的价值的一种方式8是氢; B1(0xFF的)
。
One way to represent a bitset with a value of 11111111
2 is std::bitset<8> b1(0xff)
.
另外,你可以从一个二进制字符串构造一个bitset:
Alternatively, you can construct a bitset from a binary string:
std::bitset<8> b1(std::string("01100100"));
std::bitset<8> b2(std::string("11111111"));
这篇关于为什么的std :: bitset的&LT; 8'变量无法处理11111111?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!