What does ~ do in C#?
58 minutes ago, fpo said:What does that mean?
I don't know what camera is in binary.
Camera is a class with an integer attribute "cullingMask".
This integer is not intended to be used as a direct value but as a bitmask.
So instead of interpreting the 32 bits as a base 10 value, we consider each bit as a boolean.
In all programming languages, a boolean (bool) datatype is at least 8 bits (1 byte) because thats the smallest piece of information that your CPU can process.
So instead of using 32 boolean datatypes (which uses 32*8=256 bits), the camera class uses an integer (32 bits) where each bits represents a boolean value (either true of false).
For these kind of bitmasks, it can sometimes be usefull to have the "~" operator, which switches each bit.
In this case, a game object can be part of 0 or more layers.
Instead of using a boolean for each layer, to indicate that it is in that layer, Unity uses a bitmask.
This saves a lot of memory (32*8 - 32 = 224 bits = 28 bytes per GameObject), when you consider how many instances of GameObject you will have.
The camera bitmask indicates which layers it wants to render (if all 32 bits are set to "1" than all layers are rendered, if the first (right most) bit is set to "1" than only the first layer is rendered).
Bitmask are not only efficient in terms of memory, but also in computation.
Applying a bitmask for example takes just a single operation (bitwise &)
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now