How To Cog - Chapter 3.7 - Flags

Bits & Boolean Values

A bit is the smallest unit of numerical storage. A bit can hold either 0 or 1. This is very similar to the boolean data type which holds a true or false value. Cog doesn't use a boolean type. Instead, it uses normal 32-bit integers to hold many true-or-false values.

Cog programmers commonly refer to a number used for boolean values as a flag. Flags aren't restricted to any base (e.g., decimal, hex), they're just whole numbers.

Before reading on, you should look through a few flags pages (e.g., Cogscript Flags). The numbers are given in hex, but the point is each line in the flag table is a true or false value that requires one bit to be turned on or off in the flag.


Hex is a base for displaying a number. The number itself is just an idea of how many things there are. To write or speak a number, you have to limit yourself to a base. Base 10 (decimal) is what humans use, but computers use Base 2 (binary). Hex (base 16) is easily converted to and from binary, and is more readable than binary. When numbers are given without a base, they are in assumed to be in base 10. This gets tricky if you're doing math between bases.

The reason hex is easily converted to binary is that each digit in hex can display any combination of four digits in binary. This is because binary displays 2 values per digit while hex can display 16 (base 10). Here's a chart:

Decimal Binary Hexadecimal

Hex uses letters after it runs out of numerals to use. Look at 1, 2, 4, and 8 in that table. Each number uses only one bit. This means that you can add any combination of these numbers, and no matter what it is, you can disassemble it back to what you added up. E.g., 1 and 8 make 9. You can only subtract each of our key numbers once, so: 9 - 1 = 8. C - 4 = 8. Try it on your own.

Although you can write flags in decimal, bits don't evenly fit into decimal's digits, so doing simple addition gets complicated. Say you have the flag 0x80F. The 0x at the beginning means that the number is being displayed in hex. This is important because if you type a hex number without it, JK won't know it's in hex and will mistake it for decimal or a variable name.

The F means that the first digit has all four bits on - these would be listed as 0x1, 0x2, 0x4, and 0x8 in the flags tables. The third digit only has the last bit turned on. By itself, it would look like 0x800.

Using Flags

There are many different types of flags in JK. Each type has a name like Actor Flags, Thing Flags, Physics Flags, etc. Thing Flags, for example, are assigned to players and store settings that relate to every type of thing that JK works with (e.g., weapons, powerups, players).

These flags can be assigned with the verb SetThingFlags() and cleared with the verb ClearThingFlags(). SetThingFlags() doesn't reset the flag, it just adds to it. You need to use ClearThingFlags() to turn off bits in a flag. If you need to retrieve the value of a flag, you'd use GetThingFlags().

Flags are simple to use, and they don't require you to know much about hexadecimal or base math.

Previous: Threads Up: How To Cog Next: Parsing
  • Create:
This page was last modified 16:56, 4 October 2006.   This page has been accessed 2,212 times.