Culling Mask Script References - <<

Good evening/afternoon to everyone,

can someone tell me what is the meaning of this:

“camera.cullingMask = 1 << 0;”.

Computers store information in binary, a series of 0s and 1s.

In particular, integer values are stored using a particular format of counting that looks like this:

0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
...and so on

The left shift operator, <<, takes all the bits in a value and “shifts” them left by some number of places.

This sort of math is useful when creating a “bitmask”, such as the camera’s culling mask. Setting the 1st, 4th, and 7th bits of the mask means the camera will render layers 1, 4, and 7.

Since the number 1 only sets a single bit, you can shift it left by any number of places to activate that bit, specifically:

1 << 0   0001 (notice we effectively count from the "0th" bit)
1 << 1   0010
1 << 2   0100
1 << 3   1000

Suppose you want a layer mask that targets only your “GUI” layer:

int mask = 1 << LayerMask.NameToLayer("GUI");

If you’re still confused, Google around for some good tutorials on bitmasks and bitwise operators. It’s an advanced programming topic, but a good one to understand if you’re interested in certain areas.