How is this unsigned short so big?


I am just testing some ideas for a program I need to make for school.
I am wondering why I am able to output a number bigger than the 65553 that an unsigned short is limited to.
for example: If I input 65535
the output is 131070



shouldn't it not work at all for that value??


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23



#include <iostream>
#include <fstream>
#include <sstream>
int main(int argc, char** argv)
{
    using namespace std;

    unsigned short i, b;
    string s;
    stringstream out, in;

       cin>>i;
       out<<i;
       s=out.str();
       in<<s;
       in>>b;
       cout<<b*2;

  return 0;
}
b*2 will give you an int which is big enough to hold the value 131070.
Last edited on
Yeah, implicit widening casts can jump out at you from nowhere. Cast it back to a short:
cout << short(b*2);
Thanks so much, that was it.
Topic archived. No new replies allowed.