int, short, long, long long类型的范围

关于带符号与无符号类型:整型 int、short  和  long 都默认为带符号型。要获得无符号型则必须制定该类型为unsigned,比如unsigned long。unsigned int类型可以简写为unsigned,也就是说,unsigned后不加其他类型说明符就意味着是unsigned int。

一字节表示八位,即:1byte = 8 bit;

《C和指针》中写过:long与int:标准只规定long不小于int的长度,int不小于short的长度。
 

类型名称          字节数      取值范围
signed char        1         -2^7 ~ 2^7-1    -128~+127

short int          2         -2^14 ~ 2^14-1  -32768~+32767

int                4         -2^31 ~ 2^31-1  -2147483648~+2147483647
unsigned int       4         0 ~ 2^32-1      0 ~ 4294967295

long int           4         -2^31 ~ 2^31-1  -2147483648~+2141483647   (同int)
unsigned long      4         0 ~ 2^32-1      0~4294967295

long long int      8         -2^63 ~ 2^63-1  -9223372036854775808~+9223372036854775807
unsigned long long 8         0 ~ 2^64-1      18446744073709551615


__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615

特别需要注意的是:int类型与long类型表示的的数据范围是一样的,所以当int的表示范围不能做到时,用long也并不能解决问题,这时候需要用long long类型。

PS:表示取值范围的上限或者下限时可以使用左移运算符(<<)实现。比如:

int的取值范围可表示为:-(1 << 31) ~ ((1 << 31) - 1) 

左移一位相当于乘2;左移运算符的优先级比加和减的优先级低!!
 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值