p ^= 1;
This is a binary-safe Exclusive-OR assignment operator. First, it computes the value of p XOR 1 (p = p ^ 1) and then stores that value in p.
If we look at the truth table of exclusive-or (XOR)
a | b | a XOR b |
0 | 0 | 0
|
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
p = p ^ 1; p = !p; p = p ? false : true; if(p) p = false; else p = true;So if you want to toggle something ON and OFF, let's say when a user clicks a button, this method is extremely short and useful.
No comments:
Post a Comment