abs*_*rge 5 c++ io stream bitset
我正在研究机器模拟程序.我有一个主存储器的位集向量,因此我可以使用指向此向量的指针pMemory-> at(i)来访问任何特定的"单词".我真的更喜欢矢量的bitset设计,而且我坚持使用它(这个程序应该在...大约6个小时,eek!)
我一直在努力弄清楚如何在不同的位置(模拟寄存器和其他存储器位置等)中进出bitset,所以我已经阅读了一些关于使用流的信息.我想出来了:
#include <bitset>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
/** demonstrating use of stringstream to/from bitset **/
{
bitset<12> sourceBits(std::string("011010010100"));
bitset<12> targetBits(0);
stringstream iBits(stringstream::in | stringstream::out);
iBits << sourceBits.to_string();
cout << targetBits << endl;
iBits >> targetBits;
cout << targetBits << endl;
} //end stringstream to/from bitset
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以,这是有效的,我可以调整这种技术以适应我的程序.
我的问题是,这是一个好主意吗?在使用bitset >>和<<运算符时,我是否缺少一些基本的东西?真的有必要做所有这些手动争吵吗?
另外,在切向上,将12位bitset复制到16位bitset时该怎么办?
谢谢,stackoverflow!这是经过我这个社区的第一个问题很多 google搜索.我感谢大家的见解!
你正在思考这个问题.要将一个位集的值复制到另一个位集,请使用赋值运算符.
#include <iostream>
#include <bitset>
int main () {
std::bitset<12> sourceBits(std::string("011010010100"));
std::bitset<12> targetBits(0);
targetBits = sourceBits;
std::cout << targetBits << "\n";
}
Run Code Online (Sandbox Code Playgroud)
bitset::to_ulong:
#include <iostream>
#include <bitset>
int main () {
std::bitset<12> sourceBits(std::string("011010010100"));
std::bitset<16> sixteen;
sixteen = sourceBits.to_ulong();
std::cout << sixteen << "\n";
}
Run Code Online (Sandbox Code Playgroud)