简体   繁体   English

错误:无效的二进制转换操作数(bitset<8> 和 int)

[英]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.

相关问题 QTextStream 的无效操作数到二进制转换 - Invalid Operands to Binary Conversion for QTextStream 错误:C ++中类型为“ float”和“ int”的二进制“ operator%”类型的无效操作数 - error: invalid operands of types 'float' and 'int' to binary 'operator%' in c++ 错误:类型为&#39;const char [3]&#39;和&#39;int *&#39;的无效操作数为二进制&#39;operator *&#39; - Error: invalid operands of types 'const char [3]' and 'int*' to binary 'operator*' 错误:二进制运算符&#39;/&#39;的类型为&#39;int&#39;和&#39;float *&#39;的无效操作数 - Error: invalid operands of types 'int' and 'float*' to binary operator '/' 错误:类型为“int [1]”和“float”的无效操作数转换为二进制“operator*” - error: invalid operands of types 'int [1]' and 'float' to binary 'operator*' “错误:二进制&#39;operator &lt;&lt;&#39;|的类型为&#39;int&#39;和&#39;const char [2]&#39;的无效操作数” - “error: invalid operands of types 'int' and 'const char [2]' to binary 'operator<<'|” 错误消息:“float”和“int”类型的无效操作数转换为二进制“operator%” - Error Message : Invalid operands of types 'float' and 'int' to binary 'operator%' 'int' 和 'int [3]' 类型的无效操作数到二进制 'operator*' - invalid operands of types ‘int’ and ‘int [3]’ to binary ‘operator*’ 为什么我收到这个错误? 错误:类型为“int”和“int(int, int)”的无效操作数转换为二进制“operator/” - Why am i Getting this error? error: invalid operands of types 'int' and 'int(int, int)' to binary 'operator/' 错误:二进制表达式的操作数无效 - error: invalid operands to binary expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM