【问题标题】:What does "^=" in c++ mean? [duplicate]c++中的“^=”是什么意思? [复制]
【发布时间】:2018-06-24 18:39:52
【问题描述】:

我在这篇文章中阅读了遗传算法的变异: c++ Genetic Algorithm Mutation error

^=Child1.binary_code[z] ^= 1 中是什么意思? 提前谢谢你。

【问题讨论】:

  • 你知道吗? += 有吗?那你知道^ 操作符是做什么的吗?然后你应该能够弄清楚^= 做了什么。如果没有,那么我建议你get a good beginners book or two to read
  • 按位异或赋值运算符。
  • Exact duplicateSymbol Hound 被证明是有用的。
  • 除了符号猎犬之外,搜索运算符优先级表几乎适用于所有语言。搜索字符名称(插入符号)和语言名称通常也可以产生相关结果,尽管在这里,它可能包含一些 C++/CLI,因为插入符号在那里很重要。

标签: c++


【解决方案1】:
Bitwise exclusive OR and assignment operator.  e.g. C ^= 2 is same as C = C ^ 2

真相表

(First Value) (Second Value)  (Result) 
  true           true           false 
  true           false          true 
  false          true           true 
  false          false          false 

【讨论】:

    【解决方案2】:

    ^ 符号在 C++ 和大多数编程语言中代表 Bitwise Xor。请参阅示例代码真值表以获得更好的解释。 a^=b 也是 a = a^b 的简写。

    示例代码

    #include <iostream>
    using namespace std;
    
    int main() {
        int a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
        cout<<int(a^b); // The result is 12(00001100)
        return 0;
    }
    

    Live Code

    真值表

    【讨论】:

      【解决方案3】:

      对于任何二元运算符符号 ⧋,x ⧋= y 表示计算 x ⧋ y 并将其存储到 x(在 x 中的中间计算只完成一次)。

      所以⧋是^(按位异或)运算符,你的东西和

      一样
      Child1.binary_code[z] = Child1.binary_code[z] ^ 1
      

      换句话说,Child1.binary_code[z] 的最低有效位被翻转。

      【讨论】:

        猜你喜欢
        • 2019-09-12
        • 2011-03-29
        • 1970-01-01
        • 1970-01-01
        • 2019-12-08
        • 2015-06-25
        • 1970-01-01
        • 2013-02-12
        相关资源
        最近更新 更多