short转为int

clleady 2009-08-05 08:06:02
unsigned short a=65536;int b;
printf("%d\n",b=a);

输出为什么是0呢?我感觉65536-65535=1说明溢出一位,那么应该是-1呀?
...全文
1344 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tompaz 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 supermegaboy 的回复:]
引用楼主 clleady 的回复:
unsigned short a=65536;int b;
printf("%d\n",b=a);

输出为什么是0呢?我感觉65536-65535=1说明溢出一位,那么应该是-1呀?


你有两个关键的条件没有说,就是你的环境里面short和int类型的大小,你代码的结果跟它们的长度有关。

从你的结果来看,我就假设你的环境short和int都是16位的。

在c/c++中,整数常量是int类型,65536已经超出了16位unsigned short的表示范围,标准规定要如下转换这个值:65536+ USHRT_MAX + 1,就是65536 + 65535 + 1,用十六进制表示这个加法:

10000 + FFFF + 1 = 100000

多于16位的进位被忽略,因此结果就是0,就是说

unsigned short a=65536; 这一句实际上是

unsigned short a = 0;

而0在16位int的表示范围内,在b = a的过程中没有发生转换,因此打印的最后结果就是0了。摘录标准的内容:

6.3.1.3 Signed and unsigned integers

1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.49)
[/Quote]
不知道什么编译器有16位的short,int,难道是单片机之类的?
fx397993401 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 supermegaboy 的回复:]
引用楼主 clleady 的回复:
unsigned short a=65536;int b;
printf("%d\n",b=a);

输出为什么是0呢?我感觉65536-65535=1说明溢出一位,那么应该是-1呀?


你有两个关键的条件没有说,就是你的环境里面short和int类型的大小,你代码的结果跟它们的长度有关。

从你的结果来看,我就假设你的环境short和int都是16位的。

在c/c++中,整数常量是int类型,65536已经超出了16位unsigned short的表示范围,标准规定要如下转换这个值:65536+ USHRT_MAX + 1,就是65536 + 65535 + 1,用十六进制表示这个加法:

10000 + FFFF + 1 = 100000

多于16位的进位被忽略,因此结果就是0,就是说

unsigned short a=65536; 这一句实际上是

unsigned short a = 0;

而0在16位int的表示范围内,在b = a的过程中没有发生转换,因此打印的最后结果就是0了。摘录标准的内容:

6.3.1.3 Signed and unsigned integers

1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.49)
[/Quote]

学习了
发现心流 2009-08-05
  • 打赏
  • 举报
回复
unsigned short 的范围就是 0 - 2^16-1
65536 就是 0 啊
bladesoft 2009-08-05
  • 打赏
  • 举报
回复
溢出啦 如果short4个字节的话,unsigned short最大表示65535,溢出后,所有位全部是零;如果两个字节,就更溢出了。
czj19891224 2009-08-05
  • 打赏
  • 举报
回复
2进制表示65536的话是111....1+1=100.......0,但此处1是不存在的,超出了16位,1在第17位了。。。最高位还是0。。。。。所以答案是000。。。0即0
飞天御剑流 2009-08-05
  • 打赏
  • 举报
回复
[Quote=引用楼主 clleady 的回复:]
unsigned short a=65536;int b;
printf("%d\n",b=a);

输出为什么是0呢?我感觉65536-65535=1说明溢出一位,那么应该是-1呀?
[/Quote]

你有两个关键的条件没有说,就是你的环境里面short和int类型的大小,你代码的结果跟它们的长度有关。

从你的结果来看,我就假设你的环境short和int都是16位的。

在c/c++中,整数常量是int类型,65536已经超出了16位unsigned short的表示范围,标准规定要如下转换这个值:65536+ USHRT_MAX + 1,就是65536 + 65535 + 1,用十六进制表示这个加法:

10000 + FFFF + 1 = 100000

多于16位的进位被忽略,因此结果就是0,就是说

unsigned short a=65536; 这一句实际上是

unsigned short a = 0;

而0在16位int的表示范围内,在b = a的过程中没有发生转换,因此打印的最后结果就是0了。摘录标准的内容:

6.3.1.3 Signed and unsigned integers

1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.49)
xixuer_20070803 2009-08-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 darkchampion 的回复:]
unsigned 怎么会有-1呢
[/Quote]
UP!
pmerOFc 2009-08-05
  • 打赏
  • 举报
回复
你的系统 short 和 int 是几个字节?
jixingzhong 2009-08-05
  • 打赏
  • 举报
回复
0 比较特殊一点
DarkChampion 2009-08-05
  • 打赏
  • 举报
回复
unsigned 怎么会有-1呢
DarkChampion 2009-08-05
  • 打赏
  • 举报
回复
65536=0x10000
对a来说已经溢出了,这里是截断,a=0x0000
xylicon 2009-08-05
  • 打赏
  • 举报
回复
因为65536 相当于 unsigned short 的0值。

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧