I am working on a Hill Climber Algorithm and I need to represent data as bitsets.
To sumarize my issue, I have written this piece of code:
#include <iostream>
#include <boost/dynamic_bitset.hpp>
void print(const boost::dynamic_bitset<> bitset)
{
std::cout << bitset[0]; //bitset.at(0) - same result
}
int main()
{
boost::dynamic_bitset<> myBitset(4, 10); // will be 1010
std::cout << myBitset<<"\n";
print(myBitset);
return 0;
}
I can't understand why this prints
1010
0
As indicated in the comments, boost::dynamic_bitset
indexes from the least-significant bit, i.e. the rightmost bit in the usual printed representation of a binary number. So bitset[0]
is the zero on the far right of 1010
, and the 1
that you expect to be printed is in fact bitset[3]
.