我正在使用 bitset 并提高代码的性能,我想将其更改为动态 bitset,但是在阅读了一些与此相关的帖子后,我仍然不知道如何定义我的代码。
所以我附上了我的代码,我想知道你们中的任何人是否可以帮助我给我一些关于我应该修改什么以及如何修改的想法。
提前致谢 :)
// Program that converts a number from decimal to binary and show the positions
// where the bit of the number in binary contains 1
#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
unsigned long long int dec;
bitset<5000> binaryNumber;
bitset<5000> mask;
mask = 0x1;
cout << "Write a number in decimal: ";
cin >> dec;
// Conversion from decimal to binary
int x;
for (x = 0; x < binaryNumber.size(); x++)
{
binaryNumber[x] = dec % 2;
dec = dec / 2;
}
cout << "The number " << dec << " in binary is: ";
for (x = (binaryNumber.size() - 1); x >= 0; x--)
{
cout << binaryNumber[x];
}
cout << endl;
// Storage of the position with 1 values
vector<int> valueTrue;
for (int r = 0; r < binaryNumber.size(); r++) //
{
if (binaryNumber.test(r) & mask.test(r)) // if both of them are bit "1"
// we store in valueTrue vector
{
valueTrue.push_back(r);
}
mask = mask << 1;
}
int z;
cout << "Bit 1 are in position: ";
for (z = 0; z < valueTrue.size(); z++)
{
cout << valueTrue.at(z) << " ";
}
cout << endl;
system("pause");
return 0;
}