bitset<> consumes input in the reverse order.

A

Artemis Fowl

when i used bitset<> to accept data from the user, much to my surprise,
i got the wrong answer. After a bit of fiddling, figured out that the
data being entered was being consumed in reverse. I mean,

when 1010110001 was entered, the variable contained 1000110101 from the
lowest index to the highest.

Is there any way by which i can retain the order of input in bitset<>
variable itself?
Or am i asking for too much.. :(
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Artemis said:
when i used bitset<> to accept data from the user, much to my surprise,
i got the wrong answer. After a bit of fiddling, figured out that the
data being entered was being consumed in reverse. I mean,

when 1010110001 was entered, the variable contained 1000110101 from the
lowest index to the highest.

Is there any way by which i can retain the order of input in bitset<>
variable itself?
Or am i asking for too much.. :(

How do you 'accept data from the user' ?

Some code, please, to show your problem.

Stefan
 
A

Artemis Fowl

int main(){
bitset<8> plaintext;
bitset<10> key;
cout<<"Enter the 8 bit plaintext data to be encrypted in Binary :
"<<flush;
if(cin>>plaintext){
cout<<"Enter the 10 bit key in Binary : "<<flush;
if(cin>>key){
/*call constructor*/
}
}
}
Thanks in advance :)
-AF
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Artemis said:
when i used bitset<> to accept data from the user, much to my surprise,
i got the wrong answer. After a bit of fiddling, figured out that the
data being entered was being consumed in reverse. I mean,

when 1010110001 was entered, the variable contained 1000110101 from the
lowest index to the highest.

Is there any way by which i can retain the order of input in bitset<>
variable itself?
Or am i asking for too much.. :(

(Now I'm with you... )

The answer is: That's the way bitset<> and binary numbers work.
If you write (binary) %1010 the bit with the lowest index is
the rightmost bit (a '1').
(this is in contrast to for example a char[], where char[0] is expected to
be the leftmost char)

So this code:

// -----------------------------
bitset<4> b(string("1010"));

for(size_t n=0; n<b.size(); ++n)
cout << b[n]
// -----------------------------

gives "0101" as output.


Stefan
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Stefan said:
(Now I'm with you... )

The answer is: That's the way bitset<> and binary numbers work.
If you write (binary) %1010 the bit with the lowest index is
the rightmost bit (a '1').

(Not my day today..)

I meant of course:

"If you write (binary) %1010 the bit with the lowest index is
the rightmost bit (a '0')."
(this is in contrast to for example a char[], where char[0] is expected to
be the leftmost char)

So this code:

// -----------------------------
bitset<4> b(string("1010"));

for(size_t n=0; n<b.size(); ++n)
cout << b[n]
// -----------------------------

gives "0101" as output.


Stefan


Stefan
 
A

Artemis Fowl

Thanks for the info.
Looks like i will have to brew some other method to fetch input.
Any suggestions?
Accepting the input and reversing the number sounds a bit abberant. :(
Also, is there any disadvantage if i use vector<bool> apart from the
fact that i will miss out in the AND,OR and XOR functions?

-AF
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top