我正在研究 bitset,所以我想知道将 base-10 转换为 base-2 的最佳方法是什么,因为出于某种原因我得到了错误的答案: 给出数字 19,我希望看到: 10011(16-2-1),为什么会输出(00011)
#include <iostream>
#include <bitset>
using namespace std;
int main() {
bitset<sizeof(int)> temp(19);
for (int x = 4; x>=0;x--)
cout << temp[x];
cout << endl;
system("pause");
return 0;
}
最佳答案
sizeof 给出了 char 的数量,而不是 bit 的数量。 sizeof(int) 很可能是 4,因此 temp[4] 是 UB。
使用 bitset<sizeof(int)*CHAR_BIT>
.
关于输出的c++ bitset问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6245047/