我已經使這個代碼存儲了以二進制序列輸入的每個位1的位置。該程序的輸出不是它所期望的。我得到的10100的輸出是0x7fff9109be00。下面是代碼:一個C++程序,存儲每個位1的位置在二進制序列中
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset <5> inpSeq;
int x = 0;
int xorArray[x];
unsigned int i;
cout << "Enter a 5-bit sequence: \n";
cin >> inpSeq;
for (i = 0; i < inpSeq.size(); i++)
{
if (inpSeq[i] == 1)
{
x = x+1;
xorArray[x] = i;
}
}
cout << xorArray << "\n";
}
更新爲清楚:我腦子裏想的是什麼是「清點< < xorArray」將位打印1的位置。
您希望我們去猜測什麼是需要的,否則你」是否會在午夜告訴我們? –
我想,您正在打印陣列的內存地址。 – gravitas
對不起,如果我還不夠清楚。輸出是xorArray,它應該是序列中位1的位置列表。所以如果我輸入了10100,輸出應該是2和4. –