Many developers don’t bother with bitwise operators, mostly because people don’t know them well enough. There is also the argument of where they fit in everyday programming, like web development. There is a high likelihood people will confuse
a & b for
a && b, the first will treat operands as bits (32 bits to be precise) and the second is your everyday Boolean operation. A quick refresher on truth tables is in order here, to fully understand bitwise operators. For the sake of simplicity and brevity, let’s focus on the most important 2,
OR (click here to learn more).
Assuming we have two operands each having a single bit (1 or 0), this operator will return 1 if and only if both operands are 1s. Common operators used to denote the
AND operation are
To put it in simpler terms, if
B are both
1 then the result is
1, if either operand is not
1 the result is
Using the same variable as the
AND example, the
OR operator will return 1 either on or the other operand is 1.
How is this truth table thing useful to you?
First things first, those operators also work on operands with more tha one bit, for example,
1010 & 1000 = 1000 or
If we wanted to build a simple ACL. Typically, you would create a list of permissions (for example, Create, Read, Update and Delete) and create a role that you could assign to a user.
This will work, but can get complicated quickly especially if we need to combine permission this way. Let’s not get started on how much storage this will consume if you were building something like a CMS. Enter bit field flags. It is possible to represent these permissions using bits.
Quick comparison, “READ” is 4 bytes and in the bitwise example
AdminRole, with all four permissions is 32 bits. There is the case of bitwise operations also being multiple times more efficient that string comparison.
There are many other use cases for bitwise operations, they can be very powerful tools to add efficiency and improved readability to your programs. Not to mention there are many other bitwise operators not covered in this post.