Flags beginner to intermediate: Part-2

Tanim-ul Haque Khan
2 min readMar 26, 2019

If you haven’t read part-1 here’s the link.

Now in the last part I showed how integers can be used to make a state machine that holds multiple flags. But we should make a simplified way to interact with it. We don’t really want to memorize each value for the combinations. We at best want to know the values of the normal states.

Here’s the state’s from previous article so that you don’t need to jump around for references.

0= nothing [standard practice]

2⁰=1= length


3= length, upper


5=length, lower

6=upper, lower

7=length, upper, lower


9=length, numeric

10=upper, numeric

11=lower, numeric

12=length, upper, lower

13= upper, lower, numeric

14= length, upper, numeric

15= length, upper, lower, numeric

The base flags are


That’s all we might want to remember. We want the system to handle the rest.

I wonder if you all remember bitwise operator. If not please go and google it. We will be using OR, And, NOT operators here. First I’ll breakdown the integers into 4 bit binary format.






Adding Flag

Now if you remember what OR operator does then, Lets do this operation 1|2[1 or 2]


=0011=3=Length,Upper[Check the table from above]

So it seems like if we OR two flags we get the value of it’s combination

if you or all 4 of them.

1|2|4|8=15= length, upper, lower, numeric

lets try 2|4=0010|0100=0110=6=upper,lower.

So I guess that’s enough proof that the system works.

Detecting flag

Lets see what happens if we use and operator in the mixture.

lets take 6 which has upper and lower. if we try using and with upper and lower we would get that specific bit.

0110 & 0010 = 0010 = 2 = upper.

So if you wish to check if your state machine has specific state all you gotta do is And with your desired state and check if that state is the output.

What if you wish to remove a flag?

In case of removal we need to and with the states not form.

for example if we wish to remove upper flag from 6=upper,lower what we would do is and with not 2(!2).

6 & !2=0110 & 1101 = 0100=4=Lower.

Cool so how do I put this in code?

First lets make a class and wrap it up with a few functions.

This is a C# Code. You can use this class for a series of 32 flags.

But I still don’t wanna memorize numbers

Hmm using this directly will still be sort of stupid for production ready application. What we need to do is wrap this with enum. For that, Stay tuned for the next part.



Tanim-ul Haque Khan

Author — “How to Make A Game” / Apress, Springer Nature | Head Of Unity Department at Brain Station 23 Limited | Co-Founder of Capawcino Cat Cafe