By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,739 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

bitset<> consumes input in the reverse order.

P: n/a
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.. :(

Oct 28 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Artemis Fowl wrote:
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
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Oct 28 '05 #2

P: n/a
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

Oct 28 '05 #3

P: n/a
Artemis Fowl wrote:
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
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Oct 28 '05 #4

P: n/a
Stefan Nwe wrote:
Artemis Fowl wrote:
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').


(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
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Oct 28 '05 #5

P: n/a
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

Oct 28 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.