Set Flags and bitmasks


SetFlags
Set Flags shows how to store multiple boolean flags in a single integer value. In programming, this is known as a bitmask or as bit flags, and the idea is based on the binary number system where each digit can be only 0 or 1 (on or off, true or false).

For example, in the binary number 1010, you have four bit flags: flags 0 and 3 are false, and flags 1 and 4 are true. The decimal equivalent of 1010 is 10 (21 + 23).

Binary Decimal Power of 2
00000001 1 20
00000010 2 21
00000100 4 22
00001000 8 23
00010000 16 24
00100000 32 25
01000000 64 26
10000000 128 27

9 thoughts on “Set Flags and bitmasks

  1. thanks for this explanation ! but i don’t understand your example about the binary number “1010”. You’re talking about four bit flags from 0 to 4, that’s five? no ? but i’m not a coding expert, so i’m perhaps totally wrong there ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s