Flags beginner to intermediate: Part-1

Tanim-ul Haque Khan
4 min readMar 26, 2019

This is going to be big article so I’m going to break it down in parts.

What is a flag?

From my experience I think my readers understand better from example than theories. I’m going to take a very small scope where we need to use flags. Lets take Password validation method. We need to check if a password has uppercase,lowercase,character count,numeric.

Reference : https://stackoverflow.com/questions/5859632/regular-expression-for-password-validation

The bool states are a representation of flag.

So here we have 4 Boolean flags to know exactly which conditions were not fulfilled.[In case you were thinking of only one flag, you can’t possibly use one to store states of which conditions were false]

Now that I have your attention for the necessity of Booleans lets forget about the password function for now. Lets focus on the bools only.

While this is a great way to handle flags , in bigger tasks this will get very hard to manage. what if we had 100 flags? would you manually check all of those?You are probably thinking of a boolean array now. Well that’s a good idea too. You could map all required flags in indexes of an array. While it’s good for iteration it’s still not very good for our memory optimization. Because in the end it’s going to have an array of booleans. More or less the same amount of space will be required.

Now lets take a step back. Lets take a look at our primitive types.

A bool is basically a special type of int that takes only two values 0 and 1.But takes 1 byte. But to store 0 or 1 we only need 1 bit. We are wasting 7 bit each time we use an bool.

What if we could use it some how?

An int on the other hand can store from 0 to 2³². Taking 4 bytes.

For example let’s say we use numbers to mark flags.

Length = 1

Upper = 2

Lower = 3

Numeric = 4

Now here’s a problem. We can certainly mark one flag using the values. But it can not contain more than one value yet. But that is what we want.

So let’s try this,

5 = length, upper

6 = length, lower

7= upper, lower

8= length, upper, lower.

And so on. You get the point.

Now there are more values to mark multiple states. But this is still not good enough. I mean only you can know which values mean what. That’s not a good way to code.

If you noticed that we need a system to store a value and their combinations.

I’m going to directly give the solution to the problem. If you want to do some brainstorming stop reading and think about it. Otherwise follow along.

If you number your values in the power of 2 , you can store all possible combinations in between.

Like this.

0= nothing [standard practice]

2⁰=1= length

2¹=2=upper

3= length, upper

2²=4=lower

5=length, lower

6=upper, lower

7=length, upper, lower

2³=8=numeric

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

This way you can have a combination of flags in a standard way.

And all these information is contained in 32 bits or 4 bytes while utilizing every bit.

If you used 4 bool you would have wasted 4x7 bits to begin with. Also you could not store the combination of states . 4 bools can still be justified but an int can store 32 flags in 4 bytes. But bools would need 32 bytes to store 32 flags. Not to mention all the bits that will be wasted too.

Why are we saving bits again?

Now we have Gigabytes of ram and hard drive why do we need to save bits? Well for starters you would not always program for PC. Since IOT’s are coming up you would soon end up coding for embedded devices. Or small devices that doesn’t have much memory. You will thank me later.

Aside that, If you follow along the series you will see we end up making a more readable system than just using bools.

This will be the end of part-1. Next I’ll show a good way to update the values. Come on you weren’t planning to update all these combinations manually, were you?

Here’s the 2nd part, Link.

--

--

Tanim-ul Haque Khan
Tanim-ul Haque Khan

Written by 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

No responses yet