一.引言
INT_MAX:0x7fffffff,末尾是7,或者(1<<31)-1,必须加括号(今个忘了),否则先算减法
INT_MIN:0x80000000,不是0x10000000末尾是8,或者1<<31;
二者均在limits头文件里,不在stdlib
int 是有符号类型。最高位为1时,为负数;为0时,为正数。
假设int为4 Byte.32bit.
int最大值 最高位为0,其它31位为1.
int最小指 最高位为1,其它31为为0.
正数的补码与原码一样,负数的补码,原码取反再加1.
0xffffffff就是-1
二.整形数的位数
1 //判断整形几位数 2 #include <iostream> 3 #include <cmath> 4 #include <cstdlib> 5 using namespace std; 6 7 int a = (1<<31) - 1;//10位 8 9 int main() 10 { 11 cout<<a<<endl; 12 int cnt = 0; 13 int temp = a; 14 //错误算法 15 while(a) 16 { 17 cnt++; 18 a >>=1; 19 } 20 21 int b = (int)(log10(temp) + 1.0); //不能y用a 22 23 cout<<"--------------"<<endl; 24 cout<<cnt<<" "<<b<<endl; 25 system("pause"); 26 }