1、關于char的取值範圍
(1)char占一個位元組,每個位元組有8位。char又分為有符号型(signed char)和無符号型(unsigned char)。
這二者的不同之處就在于有符号型将最高位作為了符号位,0代表正數,1代表負數。
無符号型的取值範圍為0-255(2^8-1),這個很容易了解,因為char型隻有8位。
有符号型的取值範圍位-128(-2^ 7-1)~127(2^7-1)
出現這種情況是因為計算機是以補碼的形式來存儲數值的,
而補碼有一條規則:正數的補碼是其本身,負數的補碼是其取反(取反時符号位不變)加一得到。
若說負0與正0則是針對原名來說的:
127:0111,1111 (反碼):1111,1111 (補碼):1111,1111
0:0000,0000 (反碼):0000,0000 (補碼):0000,0000
-1:1000,0001 (反碼):1111,1110 (補碼):1111,1111
-2:1000,0010 (反碼):1111,1101 (補碼):1111,1110
…
-127:1111,1111 (反碼):1000,0000 (補碼):1000,0001
-128:1000,0000 (反碼):1111,1111 (補碼):1000,0000
當我們對127+1的時候,所得到的結果就是-128.在我看來,主要是對于符号位的了解。當自己整理一遍後,就會對其有了深刻的認識。
(2)unsigned char範圍是0~255
執行下面這個循環的時候會發現其實他是一個死循環
for(int k = 0; k <=255; k ++)
{
unsigned char i=k;
//繼續其它。
}
K=255時,再加一則會變為0
255:1111,1111
0:(1)0000,0000
因為unsigned char類型隻有8位,多餘的一位被溢出了。
2、浮點類型的單精度值具有 4 個位元組,即32位,包括一個符号位、一個 8 位 excess-127 二進制指數和一個 23 位尾數。尾數表示一個介于 1.0 和 2.0 之間的數。由于尾數的高順序位始終為 1,是以它不是以數字形式存儲的。此表示形式為 float 類型提供了一個大約在 -3.4E+38 和 3.4E+38 之間的範圍。
注意:由于指數是以無符号形式存儲的,是以指數的偏差為其可能值的一半。對于 float 類型,偏差為 127;對于 double 類型,偏差為 1023。您可以通過将指數值減去偏內插補點來計算實際指數值。
是以float的指數範圍位float的指數範圍為-127~128
float的範圍為-2^127 ~ +2^128,也即-1.17E+38 ~ +3.40E+38
下面的程式可以測試一部分變量類型的大小:
#include<iostream>
#include<string>
#include <limits>
using namespace std;
int main()
{
cout << "type: \t\t" << "************size**************" << endl;
cout << "bool: \t\t" << "所占位元組數:" << sizeof(bool);
cout << "\t最大值:" << (numeric_limits<bool>::max)();
cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
cout << "char: \t\t" << "所占位元組數:" << sizeof(char);
cout << "\t最大值:" << (numeric_limits<char>::max)();
cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
cout << "signed char: \t" << "所占位元組數:" << sizeof(signed char);
cout << "\t最大值:" << (numeric_limits<signed char>::max)();
cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
cout << "unsigned char: \t" << "所占位元組數:" << sizeof(unsigned char);
cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
cout << "wchar_t: \t" << "所占位元組數:" << sizeof(wchar_t);
cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
cout << "short: \t\t" << "所占位元組數:" << sizeof(short);
cout << "\t最大值:" << (numeric_limits<short>::max)();
cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
cout << "int: \t\t" << "所占位元組數:" << sizeof(int);
cout << "\t最大值:" << (numeric_limits<int>::max)();
cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
cout << "unsigned: \t" << "所占位元組數:" << sizeof(unsigned);
cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
cout << "long: \t\t" << "所占位元組數:" << sizeof(long);
cout << "\t最大值:" << (numeric_limits<long>::max)();
cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
cout << "unsigned long: \t" << "所占位元組數:" << sizeof(unsigned long);
cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
cout << "double: \t" << "所占位元組數:" << sizeof(double);
cout << "\t最大值:" << (numeric_limits<double>::max)();
cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
cout << "long double: \t" << "所占位元組數:" << sizeof(long double);
cout << "\t最大值:" << (numeric_limits<long double>::max)();
cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
cout << "float: \t\t" << "所占位元組數:" << sizeof(float);
cout << "\t最大值:" << (numeric_limits<float>::max)();
cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
cout << "size_t: \t" << "所占位元組數:" << sizeof(size_t);
cout << "\t最大值:" << (numeric_limits<size_t>::max)();
cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
cout << "string: \t" << "所占位元組數:" << sizeof(string) << endl;
// << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
cout << "type: \t\t" << "************size**************" << endl;
system("pause");
return 0;
}
程式摘自:
http://www.cnblogs.com/tenlee/p/4420102.html
文章借鑒:
https://blog.csdn.net/wsccdsn/article/details/8180301
https://baike.sogou.com/v154979876.htm?fromTitle=FLOAT