【问题标题】:error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'错误:'int' 和 '<unresolved 重载函数类型>' 类型的无效操作数到二进制 'operator<<'
【发布时间】:2014-07-10 15:20:35
【问题描述】:

我想得到 z_Data 的第 48 个字符的第 6 位

{
    char c = pPkt->z_Data[47];                // this z_Data is a char buffer
    std::cout<<(c>>3)&1<<std::endl;
    std::cout<<(c>>4)&1<<std::endl;
    std::cout<<(c>>5)&1<<std::endl;
}

【问题讨论】:

  • &lt;&lt;(和&gt;&gt;)的优先级高于&amp;
  • 至于&amp;为什么优先级低:原来在C的开发中&amp;&amp;是不存在的; &amp; 也用于逻辑与。 &gt;&gt; 是移位的,因此对于意图 a &gt;&gt; b AND c&amp; 的优先级较低是很自然的。当他们添加&amp;&amp; 时,他们不想更改与&amp; 一起使用的现有代码。

标签: c++


【解决方案1】:

&lt;&lt; 的优先级高于&amp;,所以你需要:

std::cout << ((c >> 3) & 1) << std::endl;
std::cout << ((c >> 4) & 1) << std::endl;
std::cout << ((c >> 5) & 1) << std::endl;

【讨论】:

    猜你喜欢
    • 2015-12-02
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多