int型的最大值、最小值 c++

  • Post author:
  • Post category:其他




int型的最大值、最小值

C/C++中int类型是32位的,范围是-2147483648到2147483647 。

INT_MIN 和 INT_MAX
int max = (1<<31)-1;//这里要加括号,运算符优先级
int min = 1<<31;//由于int是32位的
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    
    int max_int = (1<<31) -1;
    int min_int = 1 << 31;
    
    cout<<max_int<<" "<<min_int<<endl;
    
    return 0;
}

结果

Hello, World!
2147483647 -2147483648
Program ended with exit code: 0

所以可以写成

func getMinInt {
    return 1 << 31;
}
func getMaxInt {
    return (1 << 31) - 1;
}



golang里的表示方法

// Integer limit values.
const (
	MaxInt8   = 1<<7 - 1
	MinInt8   = -1 << 7
	MaxInt16  = 1<<15 - 1
	MinInt16  = -1 << 15
	MaxInt32  = 1<<31 - 1
	MinInt32  = -1 << 31
	MaxInt64  = 1<<63 - 1
	MinInt64  = -1 << 63
	MaxUint8  = 1<<8 - 1
	MaxUint16 = 1<<16 - 1
	MaxUint32 = 1<<32 - 1
	MaxUint64 = 1<<64 - 1
)



版权声明:本文为glw0223原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。