unsigned and signed short int

I am confused with the output of my program..i am not able to understand the output.

#include<iostream>
using namespace std;
int main()
{
short int i;
short unsigned int j;

j=50000;
i=j;
cout <<i<<" "<<j;

system("pause");
return 0;
}

output: -15536 50000
You have an overflow. See the limits:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<limits>
using namespace std;
int main()
{
short int i;
short unsigned int j;

j=50000;
i=j;
  std::cout << "Minimum value for short int: " << std::numeric_limits<short int>::min() << '\n';
  std::cout << "Maximum value for short int: " << std::numeric_limits<short int>::max() << '\n';
  std::cout << "Minimum value for short unsigned int: " << std::numeric_limits<short unsigned int>::min() << '\n';
  std::cout << "Maximum value for short unsigned int: " << std::numeric_limits<short unsigned int>::max() << '\n';
cout <<i<<" "<<j;

system("pause");
return 0;
}
Minimum value for short int: -32768
Maximum value for short int: 32767
Minimum value for short unsigned int: 0
Maximum value for short unsigned int: 65535
-15536 50000


What you have is the same bit combination interpreted with a sign and without
Topic archived. No new replies allowed.