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.
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.
^
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;
}
Truth Table
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.
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