std::bitset construction with std::string question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dill Hole

    std::bitset construction with std::string question

    Can anyone tell me why

    std::bitset<2> foo(std::string ("01"))

    initializes the bitset in reverse order, i.e.

    foo[0]=true
    foo[1]=false

    I would expect the bitset to be initialized in string text order.
  • John Harrison

    #2
    Re: std::bitset construction with std::string question


    "Dill Hole" <dillhole@micro soft.com> wrote in message
    news:7brbgvs98i bjn2fcdtl7drc7t k82fstpoj@4ax.c om...[color=blue]
    > Can anyone tell me why
    >
    > std::bitset<2> foo(std::string ("01"))
    >
    > initializes the bitset in reverse order, i.e.
    >
    > foo[0]=true
    > foo[1]=false
    >
    > I would expect the bitset to be initialized in string text order.[/color]

    I wouldn't,

    6 = 110 = 1*2^2 + 1*2^1 + 0*2^0 == foo[2] is 1, foo[1] is 1, foo[0] is 0.

    In other words its a consequence of the fact that in English numbers the
    most signficant digit goes first.

    john


    Comment

    • Mike Wahler

      #3
      Re: std::bitset construction with std::string question


      Dill Hole <dillhole@micro soft.com> wrote in message
      news:7brbgvs98i bjn2fcdtl7drc7t k82fstpoj@4ax.c om...[color=blue]
      > Can anyone tell me why
      >
      > std::bitset<2> foo(std::string ("01"))
      >
      > initializes the bitset in reverse order, i.e.
      >
      > foo[0]=true
      > foo[1]=false
      >
      > I would expect the bitset to be initialized in string text order.[/color]

      See John H's reply, then observe the output of:

      cout << foo << '\n';

      -Mike



      Comment

      Working...