我正在制作机器模拟程序。我有一个主存储器的位集向量,所以我可以使用指向这个向量的指针,pMemory-> at(i)来访问任何特定的“字”。我真的更喜欢bitsets的矢量设计,我坚持它(这个程序是在...约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;
}
所以,这个工程,我可以适应这种技术,以适应我的程序。
我的问题是,这是一个好主意吗?有没有关于使用bitset >>和<运算符的错误信息?是否真的有必要做所有这些手动争论?
另外,切线方面,当将12位位集复制到16位位集时该怎么办?
谢谢,stackoverflow!这是我对使用Google搜索后的第一个问题。我感谢大家的见解!
+1进行了有益的示范项目。 – 2012-03-15 16:30:57