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

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
)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值