Search code examples
c++c++11bitsetstd-bitset

is bitset data stored in reverse order?


I was trying out std::bitset and after getting wrong results for a while I noticed that the results were in reverse order. Tried searching on cppreference page but couldn't find any source on this and hence need an confirmation. This should be default behaviour across different compilers too?

#include <iostream>
#include <bitset>
using namespace std;

int main() {
    bitset<7> bin('C');
    cout << bin << endl;
    for(int i = 0; i < 7; ++i){ cout << bin[i]; }
    return 0;
}

1000011

1100001


Solution

  • From the C++ standard:

    When converting between an object of class bitset<N> and a value of some integral type, bit position pos corresponds to the bit value 1 << pos. The integral value corresponding to two or more bits is the sum of their bit values.