Bitwise Operators Part 3

Hey folks! Continuing with the Bitwise Operators POC, this is what we’ll do:

What we’ll do

A simple page using HTML + Javascript to create any states and combine them in a rule. Once the rule is created, you’ll check if the rule is working.


For example, you can have the states:

  • Sleeping;
  • Awake;
  • Seated;
  • Standing (still);
  • Walking;
  • Running

And, combining the states, you can define some (naive) rules:

  • You can awake only if your state is sleeping;
  • You can seat only when you awake OR Standing (ok, I know, you can also seat when sleeping, bla, bla, bla… but, for this example, this is the rule, OK?);
  • You can stand up only when you’re seated;
  • You can walk only if you are standing OR running;
  • You can run only if you are standing OR walking;
  • You can fall if you are sleeping OR awake OR seated OR standing OR walking OR running.

As you could see, you’ll create simple rules with only one condition or combining with the Bitwise OR Operator.

We’ll log some things on the console to help the understanding of how the things work.

Creating the states and rules

To do this, follow the rules. It also will be helpful to look at the logs!

The states and the rules were created


Select ONE state (selecting more than one won’t work, because we don’t live in a quantic world!), a rule and click on ‘Check Rule’.

It won’t work if you try to set a ‘Quantic’ state!
The state is OK! Now, select a rule…
Ready to go! Note the status text showing the status and the rule which will be tested

Testing a simple rule

The first test is pretty simple: Can awake? The rule is: should be sleeping.

1st test

When we create the rule, we’ll iterate through the ‘multi-select’ taking the selected ones and, we’ll use the ‘Bitwise OR’ to create the rule.

As this first test is a simple one, the value of the rule will be the same value as the state.

So, we’ll test if ‘a & b’ where a is the status and b is the rule. The binary representation of the number 1 is 00000001, so, when we test using the Bitwise AND we’ll get:


Let’s remember

Performs the AND operation on each pair of bits. a AND b yields 1 only if both a and b are 1.

As 1 is ‘truthy’, yes, you can awake if you’re sleeping!

But, can you run when sleeping (dreaming you’re running is not valid, ok? ahahahah)?

Sorry, you can’t run when is sleeping!

As our log teach us, a is 00000001 and b is 24, or 00011000, so:


The result of ‘a & b’ is zero, or, ‘falsy’.

Testing a more complex rule

Now, let’s check if you can seat. The rule is: you can seat if you’re awake or standing. Awake, in this case, requires a broad interpretation, when you’re awake, you can be, for example, awake and lying in bed. So…

Status awake testing can-seat rule:

YES! You can seat if you are awake!

Why? See the logs! Awake is 2, or, 00000010 and can-seat is 10, or 00001010, so:

00000010 (or, 2!)

The result is 2, and 2 is ‘truthy’, so, you can seat if you’re awake.

Status standing testing can-seat rule:

You can seat when you’re standing!

Why? Standing is 8, or 00001000 and can-seat is 10, or 00001010, so:

00001000 (or, 8!)

The result is 8, and 8 is truthy, so, you can seat if you’re standing.

Status running testing can-seat rule:

Sorry, mate. You can’t seat when you’re running!

Why? Running is 32, or 00100000 and can-seat is 10, or 00001010, so:

00000000 (zero)

The result is 0, and 0 is ‘falsy’, so, you can’t seat if you’re running.


This is how it must work. At the next post, we’ll create all the CRUD code, the logic, and some hardcoded tests.

Stay tuned to follow this POC and Happy Coding!

Leave a Reply

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

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s