Skip to content

Understanding Bitwise Operations and Spawnflags

Derpduck edited this page Nov 16, 2020 · 29 revisions

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 = 16

For example, the weapon_item_spawn has 3 possible spawnflags:

1 : Enable Physics on spawned item
2 : Spawned Item Must Exist
8 : Infinite Items

The 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:

  1. Open the Calculator app.
  2. Click the dropdown, and select Programmer.
  1. Select DEC for decimal.
  1. Select the Bit toggling keypad.
  1. You will now see this display, each 0 represents a bit. 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.
  1. 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.

Clone this wiki locally