[英]Error: Invalid operands to binary conversion(bitset<8> and int)
How to iterate through a binary number in c++?如何在 C++ 中遍历一个二进制数?
In the mentioned function I'm getting在提到的功能中,我得到
invalid operands to binary conversion (bitset<8> and int)
二进制转换的无效操作数(bitset<8> 和 int)
This is the function in my code, which is getting the stated error这是我的代码中的函数,它得到了规定的错误
int value(int x)
{
int temp=0,counter=0;
bitset<8> i(x);
while (i != 0) {
temp = i % 10;
if(temp == 1) {
counter++;
}
i = i/10;
}
return counter;
}
To count the number of 1's in the first 8 bits of x:要计算 x 的前 8 位中 1 的数量:
int value(int x)
{
return bitset<8>(x).count();
}
To count all the bits:要计算所有位:
int value(int x)
{
return bitset<sizeof(x) * CHAR_BIT>(x).count();
}
If you have to use a loop solution: (Adaption to your solution with available functions)如果您必须使用循环解决方案:(使用可用功能适应您的解决方案)
int value(int x)
{
int counter=0;
bitset<8> i(x);
while (i != 0) {
if(i[0] == 1) {
counter++;
}
i >>= 1;
}
return counter;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.