1

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

Abhinav Gauniyal
  • 7,034
  • 7
  • 50
  • 93
  • 4
    try this. [Stackoverflow: Why are the bits of a std::bitset in reverse order?](http://stackoverflow.com/q/4975037/3764546) – Abdus Salam May 13 '16 at 04:02
  • 2
    Actually, this is a better answer: http://stackoverflow.com/questions/29483123/why-does-stdbitset-expose-bits-in-little-endian-fashion – Barry May 13 '16 at 04:12

1 Answers1

2

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.

md5i
  • 3,018
  • 1
  • 18
  • 32