Tiny But Mighty: Shrinking Boolean Overhead with Bitset Magic
- Excellent Pepple
- Jun 16, 2024
- 4 min read
Updated: Jun 27, 2024

In the modern interconnected world of software development, it is crucial to optimize the amount of data transmitted and stored in our applications/Games. Boolean flags, which are commonly used for representing states and conditions, often result in unnecessary overhead in memory and bandwidth usage. Conventionally, each boolean flag uses a full byte, even though it only requires a single bit for storage. This inefficiency is especially noticeable in networked applications where data transfer speed and resource utilization are critical. Bitsets offer a powerful solution by revolutionizing the management of boolean flags. By compactly storing multiple boolean values efficiently, bitsets reduce memory consumption and improve data transmission efficiency. This article delves into the significant impact of bitsets on reducing overhead and enhancing performance in networked environments.
What are Booleans?
Booleans in programming are variables that can have one of two values: true or false. They play a crucial role in representing binary logic and are extensively utilized in software development for controlling program flow, conditions, and states.
In various programming languages like C++, Java, Python, and others, a boolean typically uses 1 byte of memory. This is the case despite a boolean value inherently representing a single binary digit (bit) that can only be 0 or 1.
This allocation is a result of memory alignment and data structure considerations. Most computers and programming languages align variables to specific memory boundaries (often multiples of 4 or 8 bytes), and a boolean is usually padded to fit within these boundaries. Consequently, even though a boolean logically requires just 1 bit, it is assigned 1 byte in memory.
For applications with numerous boolean variables or facing memory constraints, this allocation can lead to notable inefficiencies. In situations where boolean flags are abundant—such as in network protocols, databases, or large-scale applications—this inefficient memory usage can accumulate and impact overall system performance and resource utilization.
In this article, we will delve into how bitsets provide a more efficient solution for managing boolean flags, enhancing both memory utilization and data transmission efficiency in networked environments.
What are Bitsets?
A bitset is an array of bools but each boolean value is not stored in a separate byte instead, bitset optimizes the space such that each boolean value takes 1-bit space only, so space taken by bitset is less than that of an array of bool or vector of bool. (for more info see GeekforGeeks)
It is commonly used in computer programming and digital systems to efficiently store and manipulate multiple boolean flags or binary data.
Advantages:
Space Efficiency: Bitsets use less memory compared to storing boolean variables individually, especially when managing a large number of flags.
Performance: Operations on bitsets, such as setting or testing bits, are typically faster than equivalent operations on separate boolean variables.
Simplicity: Bitsets provide a straightforward interface for managing binary data, simplifying the implementation of algorithms and data structures.
Wait...So How does this work exactly?
The best way to understand how to effectively use bitsets to represent booleans is with an example. Lets say we have a class named Player representing the player character of game we are making, amongst several fields and properties we also have some character states we want to keep track of (IsAlive, IsWalking, IsRunning, IsFalling, IsAttacking, etc...)
Now this would result in our Player class using up to 5bytes (40bits) for our Boolean states alone. This doesn’t sound like much at first but when we have a game where we have multiple instances of the Player class like in an MMO (Massive Multiplayer Online) game like World of War craft or New world where we can have thousands to hundreds of thousands of players in one world , it’s easy to see how expensive it is to use the default implementation of booleans.
Now, when we use bitsets to pack our variables, we can significantly reduce the memory footprint. By representing each Boolean state as a single bit within a bitset, we only use as many bits as there are states. For example, if we have five states, we only use 5 bits instead of 40 bits. This compact representation can drastically reduce memory usage, especially when managing a large number of instances.
For this article I will use C++ code to demonstrate how this can be implemented manually and then I’ll show an easier way to implement this using a macro I created.
Step 1:
The first step is to create an enum to store all the Boolean’s needed for your class. This can be done using any number of data types but I find the most Intuitive to be enumerated types.
Note regardless of the number of booleans or states being used you want to make sure to include a Count value to your Enum, this should be the last member of your enum. We use this to acurately know how many booleans are in this class then we pass that into the bitset to allocate its size.
From this point we have now packed our booleans with bitset and we are now ready to access the values of our states using the default set() and test() methods using our enums like below.
In an instant, a method has been devised to minimize memory usage through the utilization of bitsets. By compacting our boolean values into a single byte, we can realize substantial memory efficiency gains, particularly in situations with a high number of instances, like in MMOs. This enhancement not only decreases memory consumption but also boosts the game's performance and scalability. Embracing such effective strategies is vital in game development, as resource management can significantly affect user experience and operational expenses. Therefore, when handling multiple boolean values, it is advisable to employ bitsets to optimize memory utilization and maintain the smooth operation of your application.
Bonus Content
If you’ve read this far, thank you for sticking with me. I know this can seem like a lot to digest, but it’s a crucial optimization for high-performance applications.
As a gift to you, I wanted to provide a macro that does most of this work for you, called NET_SAFE_BOOLS(). The point of this macro is to create a simple interface for handling optimized booleans without the need to repeat lines of code. This macro wraps your boolean flags in a bitset, streamlining your code and ensuring efficient memory usage.
Usage:
Our Updated Player Class
Comments