-6

I read mutation of genetic algorithm in this post: c++ Genetic Algorithm Mutation error

What does ^= mean in Child1.binary_code[z] ^= 1? Thank you in advance.

user119420
  • 107
  • 10
  • 4
    Do you know what e.g. `+=` does? Then do you know what the `^` operator does? Then you should be able to figure out what `^=` does. If not then I suggest you [get a good beginners book or two to read](https://stackoverflow.com/a/388282/440558). – Some programmer dude Jan 16 '18 at 05:40
  • Bitwise xor assignment operator. – Alex Huszagh Jan 16 '18 at 05:40
  • 3
    RTFM [Assignment operators](http://en.cppreference.com/w/cpp/language/operator_assignment). – O'Neil Jan 16 '18 at 05:41
  • 1
    [Exact duplicate](https://stackoverflow.com/q/5174593). [Symbol Hound](http://symbolhound.com/?q=c%2B%2B+%5E%3D) proves to be useful. – user202729 Jan 16 '18 at 05:42
  • In addition to symbolhound, searching for an operator precedence table works in about every language. Searching for the character names (caret) along with the language name can also generally yield relevant results, though here, it's likely to contain some C++/CLI since carets are a big deal there. – chris Jan 16 '18 at 06:03

3 Answers3

3

^ symbol stands for Bitwise Xor in C++, and in most programming languages. Refer Sample Code & Truth Table for better explanation. Also a^=b is short hand for a = a^b.

Sample Code

#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

Truth Table

enter image description here

Ishpreet
  • 5,230
  • 2
  • 19
  • 35
2

For any binary operator sign ⧋ the x ⧋= y means compute x ⧋ y and store it into x (with intermediate computations in x done only once).

So with ⧋ being the ^ (bitwise exclusive or) operator, your thing is the same as

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

in other words, the least significant bit of Child1.binary_code[z] is flipped.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1
Bitwise exclusive OR and assignment operator.  e.g. C ^= 2 is same as C = C ^ 2

TRUTH TABLE

(First Value) (Second Value)  (Result) 
  true           true           false 
  true           false          true 
  false          true           true 
  false          false          false 
Suvam Roy
  • 963
  • 1
  • 8
  • 19