-
Notifications
You must be signed in to change notification settings - Fork 8
Understanding Bitwise Operations and Spawnflags
A bitwise operation operates on a bit string at the level of individual bits. The spawnflags property of an entity uses a bit string value to determine which flags are on and off. Each flag has a unique value that when enabled increases the spawnflags value by a set amount. An understanding of how binary and bits work is helpful for understanding this concept, however it can be shown by example.
Spawnflags use a base-2 system, or a binary numeral system. This means that each value, or bit, is a power of 2. Example:
2⁰ = 1 | 2^0 = 1
2¹ = 2 | 2^1 = 2
2² = 4 | 2^2 = 4
2³ = 8 | 2^3 = 8
2⁴ = 16 | 2^4 = 16For example, the weapon_item_spawn has 3 possible spawnflags:
1 : Enable Physics on spawned item
2 : Spawned Item Must Exist
8 : Infinite ItemsThe number represents the value spawnflags is increased by when the flag is enabled.
So a spawnflags value of 2 would mean: the item must exist.
A spawnflags value of 9 would mean: physics is enabled AND there is an infinite amount of the item.
However, the Windows 10 calculator actually has a very useful feature for visualizing this:
- Open the Calculator app.
- Click the
≡dropdown, and selectProgrammer.
- Select
DECfor decimal.
- Select the
Bit toggling keypad.
- You will now see this display, each
0represents abit. Starting from the bottom right, click one of the bits to toggle it.- The value under the bits is the power of 2 that bit is on.
- This will increase the overall value by the value of that bit.
For example, these bits represent the spawnflags value in our second example:
Now, you can experiment with this to understand bitwise operations and how to calculate the spawnflags value.