我想对各个数进行类型转换并且10进制16进制输出,但是结果看起来不太美妙,出现了一些奇怪的数字,我想知道哪里出现了问题,并且在移位运算那里不能够输出,报错了,请问是什么原因?代码应该怎么改才能正确?devC++
#include<iostream>
#include<iomanip>
#include<bits/stdc++.h>
using namespace std;
int main()
{
short a=-12345;
cout<<"----------short -12345----------"<<endl;
cout<<"10进制 "<<setbase(10)<<(int)a<<" "<<(unsigned short)a<<" "<<(unsigned int)a<<" "<<(float)a<<" "<<endl;
cout<<"16进制 "<<setbase(16)<<(int)a<<" "<<(unsigned short)a<<" "<<(unsigned int)a<<" "<<(float)a<<" "<<endl;
int b=2147483647;
cout<<"----------int 2147483647----------"<<endl;
cout<<"10进制 "<<setbase(10)<<(short)b<<" "<<(unsigned short)b<<" "<<(unsigned int)b<<" "<<(float)b<<" "<<endl;
cout<<"16进制 "<<setbase(16)<<(short)b<<" "<<(unsigned short)b<<" "<<(unsigned int)b<<" "<<(float)b<<" "<<endl;
float c=123456.789e5;
cout<<"----------float 123456.789e5----------"<<endl;
cout<<"10进制 "<<setbase(10)<<(double)c<<endl;
cout<<"16进制 "<<setbase(16)<<(double)c<<endl;
double d=123456.789e5;
cout<<"----------double 123456.789e5----------"<<endl;
cout<<"10进制 "<<setbase(10)<<(float)d<<endl;
cout<<"16进制 "<<setbase(16)<<(float)d<<endl;
cout<<"----------移位操作----------"<<endl;
cout<<"左移:"<<endl;
cout<<"10进制 "<<setbase(10)<<(a<<2)<<" "((unsigned short)a<<2)<<endl;
cout<<"16进制 "<<setbase(16)<<(a<<2)<<" "((unsigned short)a<<2)<<endl;
cout<<"右移:"<<endl;
cout<<"10进制 "<<setbase(10)<<(a>>2)<<" "((unsigned short)a>>2)<<endl;
cout<<"16进制 "<<setbase(16)<<(a>>2)<<" "((unsigned short)a>>2)<<endl;
return 0;
}
这是运行结果,我觉得在转化为unsigned int那一栏的结果有问题,看起来很奇怪,数据太大,为什么?