为什么这个程序显示以下输出?
#include <bitset>
#include <cstdio>
#include <iostream>
int main()
{
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.
//Same is the case with b1
std::cout << "b1 & b2: " << (b1 & b2) << '\n';
std::cout << "b1 | b2: " << (b1 | b2) << '\n';
std::cout << "b1 ^ b2: " << (b1 ^ b2) << '\n';
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
01000000
11000111
b1 & b2: 01000000
b1 | b2: 11000111
b1 ^ b2: 10000111
Run Code Online (Sandbox Code Playgroud)
首先,我认为头文件有问题(我使用的是MinGW)所以我使用MSVCC进行了检查.但它也显示了同样的事情.请帮忙!
m0n*_*awk 10
你发起了std::bitset一个数字 01100100和11111111.只需查看输出:
01000000
11000111
Run Code Online (Sandbox Code Playgroud)
两者都错了.你正在发起std::bitset一个十进制数11111111它101010011000101011000111的二进制形式.首先是数字01100100以八进制表示:1001000000001000000二进制.并std::bitset采取最重要的八位.
这是正确的代码:
#include <bitset>
#include <iostream>
#include <string>
int main()
{
std::bitset<8> b1(std::string("01100100")); std::cout<<b1<<std::endl;
std::bitset<8> b2(std::string("11111111")); std::cout<<b2<<std::endl;
std::cout << "b1 & b2: " << (b1 & b2) << '\n';
std::cout << "b1 | b2: " << (b1 | b2) << '\n';
std::cout << "b1 ^ b2: " << (b1 ^ b2) << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
而正确的输出:
01100100
11111111
b1 & b2: 01100100
b1 | b2: 11111111
b1 ^ b2: 10011011
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
239 次 |
| 最近记录: |