C++中將短數據類型擴展爲長類型

首先:任何數據在計算機內都是以二進制的補碼形式存儲的。

對數據的處理是對這個二進制的補碼進行的操作。

 void main()
 { 
	  
	 cout<<"將有符號的數轉換爲無符號的數:"<<endl;
	 cout<<"將short int 裝換爲 int 類型:"<<endl;
	 short int a = 0x7000;  // 28672
	 unsigned int b  = a; 
	          int c  = a;
	cout<<"b="<<b<<endl;    // 28672
	cout<<"c="<<c<<endl;    // 28672
     short int  d = 0x8000; // -32768
	 unsigned int e  = d;    // 0xffff8000
	          int f  = d;    // -32768
	cout<<"e="<<e<<endl;
	cout<<"f="<<f<<endl;

	cout<<"將無符號數轉換爲有符號的數:"<<endl;
	unsigned short int g = 0x7000; // 28672
	int h = g;         
	unsigned int i = g;
	cout<<"h="<<h<<endl;   // 28672
	cout<<"i="<<i<<endl;   // 28672

	int n = ~a;
	unsigned int q = ~a;
	cout<<"n="<<n<<endl;  //-28673
	cout<<"q="<<q<<endl;
	unsigned short int  j = 0x8000; // 32768
	int k = j; 
	unsigned int m = j;
	cout<<"j="<<j<<endl;
	cout<<"k="<<k<<endl; // -32768
	cout<<"m="<<m<<endl; // 0xffff8000;
    


 }

發佈了93 篇原創文章 · 獲贊 7 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章