473,396 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 4858
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 Näwe 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Scott Brady Drummonds | last post by:
Hi, everyone, I have a program in which I need to store a series of Boolean values. A vector<bool> would be ideal. However, I'm concerned about this data structure because of Scott Meyers'...
3
by: Alexandros | last post by:
Hi. How can I create a vector<bool> efficiently from a char* or a vector<char> ? For example, if char* c == (8,10) I want vector<bool> v to be: (0000100000001010)
1
by: Filip Kasperkiewicz | last post by:
Hi ! I need some examples of usage <bitset> class. If you can help me write filipoNO_SPAM@benchmark.pl. Thanks for all help Philip Kasperkiewicz
11
by: Charles L | last post by:
I have read that the inclusion of <fstream.h> makes the inclusion of <iostream.h> unnecessary. Is this correct? Charles L
5
by: DamonChong | last post by:
Hi, I am trying this in my code and getting strange answers. Would appreciate if someone can point out what it is i'm missing here. My thanks in advance. -------------code...
3
by: shaun | last post by:
I have a function for returning the value of a bit field in a number: template<typename T> T bitfield(const T num, const unsigned int bitStart, const unsigned int bitEnd){ T mask,...
8
by: Lionel B | last post by:
On my platform I find that the std::vector<boolspecialisation incurs a significant performance hit in some circumstances (when compared, say, to std::vector<intprogrammed analagously). Is it...
7
by: huili80 | last post by:
Should complex<T>::real() and imag() return a value or a refernce? What does the standard say about this? I just realized that MSVC2008's implementation returns a value, but in GCC reference is...
4
by: abhinuke | last post by:
So I have been working on this code where I have to partition 8 bits into two set of 4 bits each.So far I have been trying my luck with bitset<> container class in C++.But it doesnt give me any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.