Search code examples
c++stringbitbitsets

Why a C++ bitset initialized using an string is reversed?


For the first time, I initialized a bit set using a string and found out that the bits are stored in reverse order, i.e.:

bitset<3> test(string("001"));

then the bits are stored as bellow: test[0] = 1 test[1] = 0 test[2] = 0

I am not sure if I'm doing something wrong or this is the way it should be.


Solution

  • This is the way it should be. Bits stored in a bitset are ordered in such a way so that the index of a bit is the factor it is raised by.

    In other words, the value at test[0] is the 2^0 bit, test[1] is 2^1, test[2] is 2^2, etc.

    Endianness has nothing to do with it.