Bitwise comparisons are like a boolean operation on every bit of an integer.
&
is like and
|
is like or
^
is “xor” exclusive or, it is similar to the !=
operator.
So if we treat every bit as a boolean value using and
to get the final value it may look like this
0b0011 0101 (53)
& 0b1100 0101 (197)
-------------
0b0000 0101 (5)
Reading right-to-left the first bit between the numbers are both 1
, so 1
is written to the output’s first bit. Then both bits are zero, if even one number is zero a zero will be written, similar to false and true
evaluates to false
. The last four bits include a one but not on both numbers, so zeros are written. Finally we end up with a much smaller value than either number.
Since the first bit represents 1
we could check for even or odd numbers by using a bitwise and on the first bit, this is a trick most compilers will recognize and do for you as it’s really fast to use bitwise operations over modulus/division.
if (my_number & 0b1) == 0:
pass # must be even, the first bit is zero
We can see how useful bitwise AND is to Godot’s collision layer/mask system. If two objects collide we can check if their mask matches the other’s layer with a bitwise AND, if any bit matches then the collision continues.
if (self.collision_mask & other.collision_layer) != 0:
pass # hit!
Bitwise OR writes 1
to the output if either numbers has 1
0b0011 0101 (53)
| 0b1100 0101 (197)
-------------
0b1111 0101 (245)
This is particularly useful for combining “flags”, where each bit is a boolean option combining sets of options does not overwrite or add extras. I can’t find a great example within Godot, so here’s SDL’s init sequence, each SDL_INIT_*
is used to determine which sub-system needs to be loaded, without using big arrays or a long line of argument-position true/falses.
const int sdl_success = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER);
XOR is the trickiest of the bunch, a 1
is only written if the values do not match.
0b0011 0101 (53)
^ 0b1100 0101 (197)
-------------
0b1111 0000 (240)
There are plenty of uses for this, fast bucket-fill algorithms, “linear feedback shift registers”, cryptography, RNG. Not sure if it’s particularly used in Godot; looks like a lot of xor-equal assignments for flags and random number generation.