知識社群登入
如何證明 int 的範圍?
by 高顯忠, 2010-12-07 16:15, 人氣(2245)
以下的程式,可以證明 int 的範圍是

-2147483648 到 +2147483647

也就是 

(-2^31) 到 (2^31) - 1

int 是 四個 Bytes 的整數,是 32個 bits 的整數,
有一個 bit 被用來表示 正,或是 負。

剩下的 31 個 bits, 可以用來表示

0 - +2147483647

負的整數,使用 二的補數來表示。

所以,40000*60000 --> ???
其結果,是一個 負的整數。



#if 0

 b= 11, a= 10
 Press [Esc] for stop! other key for continue...

 *** exit the loop,
 b= -2147483648, a= 2147483647
 Press [Esc] for stop! other key for continue...

#endif
// ----------------------------------------------

#include "inc-01.h"
#include "sj-01.h"
#include "sj-02.h"
// ----------------------------------------------

int main()
{
int a, b;

a= 10;
b= a + 1;// b= 11, > 10, > a
printf("\n b= %d, a= %d\n", b, a);
pause();

while (b > a) {
a= a + 1;
b= a + 1;
}

// !(b > a), --> b <= a
printf("\n *** exit the loop, \n b= %d, a= %d\n", b, a);
pause();

return 0;
}// end of main()