top of page

<bit> header

Hi C++ Fans.

C++20 introduced many exciting and useful changes to C++.

In this post, I will show how to calculate the size of a bit field, and we will do it during compilation.

Pre-C++20, we had to write our pop count function that initializes the field to contain only ones and then pops them individually while counting the pops until the field becomes zero. The fun thing is that we can do everything in compile time since C++14.

C++20 introduced the <bit> header, which already implemented a very similar function for us.

All functions are also implemented as constexpr so that you may calculate the width of the bit fields during compilation.

This is a game-changer for embedded systems development, as it eliminates the need to manually manipulate bits, saving valuable time and effort. I encourage you to explore this new feature.



bottom of page