Operations Flashcards
&&
Logical and
Returns true if both statements are true
eg x < 5 && x < 10
||
Logical or
Returns true if (either) one of the statements is true
eg x < 5 || x < 4
!
Logical not
Reverse the result, returns false if the result is true
!(x < 5 && x < 10)
&
(bitwise AND)
The & operator compares each binary digit of two integers and returns a new integer, with a 1 wherever both numbers had a 1 and a 0 anywhere else
0, 0 –> 0
0, 1 –> 0
1, 0 –> 0
1, 1 –> 1
|
(bitwise OR)
the | operator is to the || operator as the & operator is to the && operator.
The | operator compares each binary digit across two integers and gives back a 1 if either of them are 1. Again, this is similar to the || operator with booleans.
0, 0 –> 0
0, 1 –> 1
1, 0 –> 1
1, 1 –> 1
~
(bitwise NOT)
(bitwise XOR)
The ^ operator is similar to the & and | operators in that it takes an int or uint on both sides. When it is calculating the resulting number, it again compares the binary digits of these numbers. If one or the other is a 1, it will insert a 1 in to the result, otherwise it will insert a 0. This is where the name XOR, or “exclusive or” comes from.
0, 0 –> 0
0, 1 –> 1
1, 0 –> 1
1, 1 –> 0