Math in Unity : Grid and Bitwise operation (Part IV)
Let’s start with the AND operator.
The sintax is this : &
Just like booleans, the operator AND checks both sequences
and returns 1 only if both bits are 1.
As always, 1 will be true and zero wil be false.
For example, if we need to check these two sequences:
1011
0011
the value that will be returned will be
0011
In practice :
1 and 1 return 1
0 and 1 return 0
1 and 0 return 0
0 and 0 return 0
The OR operator has this syntax : |
Checks both sequences
and returns 1 if any of the two bits is 1.
If we check same sequences as above:
1011
0011
the value that will be returned will be
1011
In practice :
1 and 1 return 1
0 and 1 return 1
1 and 0 return 1
0 and 0 return 0
The XOR operator has this syntax : ^
Checks both sequences
and returns 1 if the two bits are different.
Same sequences :
1011
0011
the value that will be returned will be
1000
In practice :
1 and 1 return 0
0 and 1 return 1
1 and 0 return 1
0 and 0 return 0
The last (for now) operator is NOT
The sintax is this : ~
Check one number and inverts all bits of it.
In this case, this operator checks only one sequence and REVERSES EVERY VALUE
For example if we have this
~0101
the value that will be returned will be
1010
In practice :
0 becomes 1,
1 becomes 0.
In the next lecture we see how to use this operators to set the flags!