468,294 Members | 1,953 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

bitset<> consumes input in the reverse order.

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
5 4159
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
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
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
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
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.

Similar topics

3 posts views Thread by Scott Brady Drummonds | last post: by
3 posts views Thread by Alexandros | last post: by
1 post views Thread by Filip Kasperkiewicz | last post: by
11 posts views Thread by Charles L | last post: by
5 posts views Thread by DamonChong | last post: by
3 posts views Thread by shaun | last post: by
7 posts views Thread by huili80 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.