Use std::bitset<16> and call operator[] to access individual bits:
#include <iostream>
#include <bitset>
int main()
{
std::bitset<16> bits(534);
std::cout << bits << std::endl;
//use operator[] to access individual bits
std::cout << bits[2] << std::endl;
}
Output (demo):
0000001000010110
1
This may not be most efficient but if you consider safety, then it is better alternative to raw array types. The efficiency difference will be almost negligible.
If the number of bits is not known at compile time, and can be known at runtime, then boost::dynamic_bitset will help you. Have a look at it:
From its doc,
The dynamic_bitset class represents a set of bits. It provides accesses to the value of individual bits via an operator[] and provides all of the bitwise operators that one can apply to builtin integers, such as operator& and operator<<. The number of bits in the set is specified at runtime via a parameter to the constructor of the dynamic_bitset.
The dynamic_bitset class is nearly identical to the std::bitset class. The difference is that the size of the dynamic_bitset (the number of bits) is specified at run-time during the construction of a dynamic_bitset object, whereas the size of a std::bitset is specified at compile-time through an integer template parameter.