c语言数字大小32768,c语言中int(2byte)的表示范围是为什么是-32768~32767!

1.这得从二进制的原码说起(数字逻辑--东野老师):

如果以最高位为符号位,二进制原码最大为0111111111111111=2的15次方减1=32767

最小为1111111111111111=-2的15次方减1=-32767

此时0有两种表示方法,即正0和负0:0000000000000000=1000000000000000=0

所以,二进制原码表示时,范围是-32767~-0和0~32767,因为有两个零的存在,所以不同的数值个数一共只有2的16次方减1个,比16位二进制能够提供的2的16次方个编码少1个。

但是计算机中采用二进制补码存储数据,即正数编码不变,从0000000000000000到0111111111111111依旧表示0到32767,而负数需要把除符号位以后的部分取反加1,即-32767的补码为1000000000000001。

到此,再来看原码的正0和负0:0000000000000000和1000000000000000,补码表示中,前者的补码还是0000000000000000,后者经过非符号位取反加1后,同样变成了0000000000000000,也就是正0和负0在补码系统中的编码是一样的。但是,我们知道,16位二进制数可以表示2的16次方个编码,而在补码中零的编码只有一个,也就是补码中会比原码多一个编码出来,这个编码就是1000000000000000,因为任何一个原码都不可能在转成补码时变成1000000000000000。所以,人为规定1000000000000000这个补码编码为-32768。

所以,补码系统中,范围是-32768~32767。

因此,实际上,二进制的最小数确实是1111111111111111,只是二进制补码的最小值才是100000000000001,而补码的1111111111111111是十进制值的-1。

{另外:补码向原码的转换过程是:正数不变,负数保留符号位的1,其它位取反加一.

补码的计算方式是1.其符号位与数值部分一起参加运算。

2.补码的符号位相加后,如果有进位出现,要把这个进位舍去(自然丢失)。

3.用补码运算,其运算结果亦为补码。在转换为真值时,若符号位为0,数位不变;若符号位为1,应将结果求补才是其真值。}

2.下面说个codeforces上当疼的问题,以前没有注意到:

long long intNum=-2147483648;

那么我们cout<

为什么呢?

因为有的编译器把2147483648视为(int)型

所以你要这样定义:

long long intNum=-(long long)2147483648;

那么cout<

3.还有一个编译时候warning的问题:

"warning: this decimal constant is unsigned only in ISO C90"

注意:c语言里面的常量默认是一个32位的有符号整型数。

解决方法:

1  在常数后面增加一个UL标识,或者ULL表示,如4294967295UL,这样就不会报警了

2 使用十六进制的数字,如0xFFFFFFFF

3 使用gcc -std=c99 用99标准来编译

原文:

The C90 rule that the default type of a decimal integer constant is eitherint, long, or

unsigned long, depending on which type is large enough to hold the value without overflow,

simplifies the use of constants. The choices in C99 are int, long and long long.

C89 added the suffixes U and u to specify unsigned numbers. C99 adds LL to specify long

long.

Unlike decimal constants, octal and hexadecimal constants too large to be ints are typed as

unsigned int if within range of that type, since it is more likely that they represent bit

patterns or masks, which are generally best treated as unsigned, rather than “real” numbers.

Little support was expressed for the old practice of permitting the digits 8 and 9 in an octal

constant, so it was dropped in C89.

A proposal to add binary constants was rejected due to lack of precedent and insufficient utility.

Despite a concern that a “lower-case-l” could be taken for the numeral one at the end of a

numeric literal, the C89 Committee rejected proposals to remove this usage, primarily on the

grounds of sanctioning existing practice.

搞明白了吧,出现这个告警,其实也是gcc提醒你该升级你的编译选项了。不过最安全的还是使用十六进制,或者加上UL或者ULL这样的说明。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值