Using bitwise operators in databases: Is it worth it?
Using bitwise operators in databases can have real advantages. Here's how to use them and what you stand to gain.

Editorial translation by Impulso.
If you're new to bitwise operators, now might be a good time to get to know them. After all, there are plenty of benefits to working with them. But first, it's important to understand a few definitions. More commonly called bitwise operators, binary operators of this kind work on the binary representation of numbers by performing operations that let us manipulate groups of bits. They're also widely used in systems where performance is critical, for example when managing permissions in file systems. But before we go into how these operators are used in databases, we should get familiar with the terminology used to define data types.
Note: In this article, we'll always index bits from right to left.
Bitwise AND (&)
The bitwise AND operation (also represented by the character "&") lets us determine which bits are set in both of two numbers. When we use the AND operator, we therefore supply two numeric inputs with the same bit width. In the example below, we'll see the AND operation applied to two bytes:
00100101
AND 00001111
= 00000101
As we can see, the result of the AND operation contains only the bits that were set in both inputs. This is quite handy when we want to check whether a particular bit is set in an input. For example, we can use the following expression to check whether bit 3 is set:
var a = 00100101 # Value to test
const b = 00000100 # Only bit 3
ativo = a & b == b
00100101
AND 00000100
= 00000100
Now let's see what happens when bit 3 is unset in the value we're testing:
var a = 00100001 # Value to test
const b = 00000100 # Only bit 3
ativo = a & b == b
00100001
AND 00000100
= 00000000
So when we test an input against a mask containing one or more bits of interest, we know those bits are set if the result equals the mask itself.
Bitwise OR (|)
The bitwise OR operation (also represented by the character "|") lets us set a bit in an existing number. The setup is similar to AND, so the same rule applies: when we use the OR operator, we supply two numeric inputs with the same bit width. In the example below, we'll use OR to set bit 3:
00100001
OR 00000100
= 00100101
As we can see from the result above, a bit only needs to be set in one of the inputs to appear in the result. In code, though, the expression would look something like this:
var dados = 00100001
const flag = 00000100
resultado = dados | flag
Why use bitwise operators in a database?
There are several reasons to use them, all aimed at improving performance. The main ones are lower storage usage and faster searches. Bitwise operators also offer two other benefits worth noting:
- We can use bitwise operators in queries, where they're very efficient;
- And we can use a single index on the attributes column, which makes searches on any of the attributes faster.
How do we query the data?
Now that we have a better idea of what bitwise operators are, how do we use them with our data? Some databases, such as MySQL and Postgres, offer specific functions and data types for working with these operators. Still, we can use bitwise operators in almost any database. To see how this works in practice, take a look at the following table of restaurant attributes and the example below:
Bit position
Decimal value
Attribute name
Base-2 representation
00000001
1
Free parking
2ˆ0
00000010
2
Smoking area
2ˆ1
00000100
4
Live music
2ˆ2
00001000
8
Vegetarian options
2ˆ3
00010000
16
Fast food
2ˆ4
00100000
32
Self-service
2ˆ5
With that in mind, let's say we want to find restaurants that offer both free parking and live music. We can use the AND operator like this:
SELECT *
FROM restaurants
WHERE attributes & 5 = 5;
We'll usually use bitwise operations in the back end to build the field value and pass it as an argument to the query:
SELECT *
FROM restaurants
WHERE attributes & ? = ?;
The same logic applies when inserting or updating data, so we always handle it in the application layer.
But is it worth it?
As with almost everything in life, the answer is: it depends. For one thing, we need to check whether the team responsible for maintenance is familiar with bitwise operators. It also depends on how many Boolean fields are involved and which programming language and frameworks are used. Of course, you'll see a performance gain in almost every scenario, but it'll only be noticeable in large databases where many Boolean fields are used as filters at the same time. Still, bitwise operators will always make for better query composition.
It's also a great way for people new to programming to get familiar with how bitwise operators work. So, what did you think? There's a whole world to explore in bitwise programming, and the best way to learn is to practice and stay in touch with others in the field. Why not drop by the Impulso community on Discord? After all, communities like this are where you can build your network and, of course, learn.
Written by an Impulser Professional. If you'd like to contribute an article, too, send an email to blog@impulso.team


