2

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.

dahma
  • 75
  • 2
  • 6
  • 2
    That's how bits are usually numbered: the bit on the right side is bit zero, because its value is 2^0. The second bit from the right is bit one because its value is 2^1. And so on. – R. Martinho Fernandes Jun 27 '13 at 12:29
  • In other words, it isn't reversed. The 0th bit is set to 1, the rest to 0, and the indexing respects that convention. – juanchopanza Jun 27 '13 at 12:44
  • Why do you want to initialize it in reversed order? – PlasmaHH Jun 27 '13 at 12:49
  • Voted to close as "unclear what you are asking". You ask why something is that isn't. – juanchopanza Jun 27 '13 at 12:58
  • @juanchopanza: Then I don't understand why in your first comment you would answer a question which you think is unclear? – dahma Jun 27 '13 at 13:29
  • It is not unclear. It is a non-question: "**Why a C++ bitset initialized using an string is reversed?**". It is not reversed, so there is no why. – juanchopanza Jun 27 '13 at 13:30
  • If I initialize something with "001" and get "100" when I print it, for example, I would say it is in reverse order. – dahma Jun 27 '13 at 13:35
  • @R.MartinhoFernandes: Why don't you make that an answer? – Axel Jun 27 '13 at 13:38

1 Answers1

3

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.

John Dibling
  • 99,718
  • 31
  • 186
  • 324