Skip to content

BitBlocks is a C++ class that packs booleans or flags into a singular byte. This open-source repository reduces storage usage in your codebase aswell as intoduces a quick way to access flags and booleans.

License

Notifications You must be signed in to change notification settings

B64-Cryptzo/BitBlocks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BitBlocks

image image

BitBlocks is a C++ single header that provides a method to pack up to 8 booleans in a single byte while also allowing you to organize booleans into enums.

Usage

banner

Include the header in your project and simply create a new class with your desired enums as the parameter. The demo shows how you can create an enum of unsigned chars to represent the boolean name.

BitBlocks menuOptions(menuAnimations | menuScaleability | menuMoveability | menuColor | menuOpen);

// Toggle booleans on and off using the built-in function
menuOptions.ToggleBit(menuOpen);

// Check the value of the boolean using bitwise operators
if (menuOptions(menuOpen) || menuOptions & menuOpen){
    ...
}

Compatibility

Compatible with both x86 and x64 due to the use of generic types. Doesn't require any other headers (i.e Windows.h etc...)

Performance

By using a single char as opposed to individual booleans, larger projects that require the use of multiple booleans can pack them in a single byte which can reduce both file size and improve performance via optimized caching. The boolean equivalent of 1 BitBlock (1 Byte) is 8 Booleans (8 Bytes)

It is worth mentioning that std::bitset can be used to accomplish the same thing however, it require a minimum of 4 bytes. Additionally, grouping booleans according to their catagory isn't possible when using a bitset without sacrificing size.

Technical Explanation

Bytes in memory are interpreted by computers as eight 1s or 0s. This allows any individual byte a max value of 256. However a boolean can only take 2 values (1 and 0). But because modern CPUs can't address anything smaller than a byte, developers represent a boolean with a full byte. This means that the 7 other bits in any boolean are wasted and unused.

Thus, by taking advantage of the base 2 nature of binary, we can assign each bit of a byte to a boolean value. This is also known as bit packing. The benefit to packing multiple booleans into a single byte is that we can reduce the amount of wasted computer capacity and theoretically reduce the latency of accessing booleans in memory by referencing a single location in memory for every 8 booleans.

Resources

  • Bit Packing

About

BitBlocks is a C++ class that packs booleans or flags into a singular byte. This open-source repository reduces storage usage in your codebase aswell as intoduces a quick way to access flags and booleans.

Topics

Resources

License

Stars

Watchers

Forks

Languages