If param_count
is known at compile time you can use std::bitset
. Here is an example:
// Define a bitmap with 'param_count + 7' elements
std::bitset<param_count + 7> b;
// Set the fifth bit, zero is the first bit
b[4] = 1;
// Convert to 'unsigned long', and the casting it to an int.
int a = int(b.to_ulong());
If param_count
is not known at compile time, you can use std::vector<bool>
. Here is another example:
// Define a bitmap with 'param_count + 7' elements
std::vector<bool> b(param_count + 7);
// Set the fifth bit, zero is the first bit
b[4] = 1;
// Convert to 'int'
int a = std::accumulate(b.rbegin(), b.rend(), 0, [](int x, int y) { return (x << 1) + y; });
The conversion from std::vector<bool>
to int
is taken from this answer.