std::bitset construction with std::string question

D

Dill Hole

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.
 
J

John Harrison

Dill Hole said:
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.

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
 
M

Mike Wahler

Dill Hole said:
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.

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

cout << foo << '\n';

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

some questions about "bitset" class 2
operator< for std::bitset 2
Question about std::move 12
Variadic templates std::tuple 2
std::bitset, standard and endianess 5
bitset 3
bitset solution 0
std::list unique 2

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top