编译不可通过?short s1 = 1;short s2 = 2;short s3 = s1 + s2; why??

崔鹏飞 2008-08-26 12:48:26
编译不可通过?
short s1 = 1;
short s2 = 2;
short s3 = s1 + s2;
why??
...全文
406 19 打赏 收藏 转发到动态 举报
写回复
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
崔鹏飞 2008-08-27
  • 打赏
  • 举报
回复
如果两个int相加的话,也有越界的可能啊.....
TorerCoder 2008-08-27
  • 打赏
  • 举报
回复
这个是面试题,我以前就遇到过!呵呵。。。贴到这里了。。。。
LovingAlison 2008-08-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 whycom 的回复:]
s1 + s2 结果为int
s3 = (short)(s1+s2);
[/Quote]
崔鹏飞 2008-08-27
  • 打赏
  • 举报
回复
那两个int相加也有可能会溢出啊.......[Quote=引用 10 楼 ojlovecd 的回复:]
引用 3 楼 cuipengfei1 的回复:
s2 s3都是short,两个short相加结果为什么是int?

很简单,比如

C# code
short a = short.MaxValue;
short b = short.MaxValue;
//a+b就会越界
[/Quote]
崔鹏飞 2008-08-27
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 cuipengfei1 的回复:]
那两个int相加也有可能会溢出啊.......[/Quote]
LuckyMouse_ZJU 2008-08-27
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 cuipengfei1 的回复:]
如果两个int相加的话,也有越界的可能啊.....
[/Quote]
int + int 就变成 long 了

你仔细去看看msdn上的隐式类型转换.
liuhz_jsmstc 2008-08-27
  • 打赏
  • 举报
回复
s1+s2的值是int型,而s3是short型,你应该显式转换一下
short s3=(short)(s1+s2);
whycom 2008-08-26
  • 打赏
  • 举报
回复
s1 + s2 结果为int
s3 = (short)(s1+s2);
vwxyzh 2008-08-26
  • 打赏
  • 举报
回复
这个问题涉及cli的定义,
参考:
Common Language Infrastructure (CLI)
Partition III
CIL Instruction Set
Final Draft, Apr 2005
---------------------------------------------------------------------------------
In the following table, “CLI Type” is the type as it is described in metadata. The “Verification Type” is a corresponding type used for type compatibility rules in verification (see §1.8.1.2.2) when considering the types of local variables, arguments, and parameters on methods being called. The column “Verification Type (in stack state)” is used to simulate instructions that load data onto the stack, and shows the types that are actually maintained in the stack state information of the verification algorithm. The column “Managed Pointer to Type” shows the type tracked for managed pointers.

CLI Type Verification Type Verification Type (in stack state) Managed Pointer to Type
int8, unsigned int8, bool int8 int32 int8&
int16, unsigned int16, char int16 int32 int16&
int32, unsigned int32 int32 int32 int32&
int64, unsigned int64 int64 int64 int64&
native int, native unsigned int native int native int native int&
float32 float32 float64 float32&
float64 float64 float64 float64&
Any value type Same type Same type Same type&
Any object type Same type Same type Same type&
Method pointer Same type Same type Not valid


These operate only on integer types. Used for and, div.un, not, or, rem.un, xor. The div.un and rem.un instructions treat their operands as unsigned integers and produce the bit pattern corresponding to the unsigned result. As described in the CLI standard, however, the CLI makes no distinction between signed and unsigned integers on the stack. The not instruction is unary and returns the same type as the input. The shl and shr instructions return the same type as their first operand, and their second operand shall be of type int32 or native int. Boxes marked X indicate invalid CIL sequences. All other boxes denote verifiable combinations of operands.

Table 5: Integer Operations
int32 int64 native int F & O
int32 int32 X native int X X X
int64 X int64 X X X X
native int native int X native int X X X
--------------------------------------------------------------------------------------
注意第一张表的Verification Type (in stack state)列,真正用于计算的是这一列的数据
根据第一张表,所有的byte,sbyte,short,ushort都会被转成int类型进行计算
再根据第二张表,int op(+-*/%) int的结果是int
把int的值赋值给short的变量,自然需要强制转换
rangeon 2008-08-26
  • 打赏
  • 举报
回复
存在从 short 到 int、long、float、double 或 decimal 的预定义隐式转换

不能将存储大小更大的非文本数值类型隐式转换为 short(有关整型的存储大小的信息,请参见 整型表(C# 参考))。例如,请看以下两个 short 变量 x 和 y:

short x = 5, y = 12;


以下赋值语句将产生一个编译错误,原因是赋值运算符右侧的算术表达式在默认情况下的计算结果为 int 类型

short z = x + y; // Error: no conversion from int to short

若要解决此问题,请使用强制转换:

short z = ( short )(x + y); // OK: explicit conversion
西安风影 2008-08-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cuipengfei1 的回复:]
s2 s3都是short,两个short相加结果为什么是int?
[/Quote]

安全考虑
s1s2是short类型,也就是两个字节,如果s1和s2都是两个字节中最大的数
那么s3用short就不能准确表示,所以是int 4个字节来表示
whycom 2008-08-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cuipengfei1 的回复:]
s2 s3都是short,两个short相加结果为什么是int?
[/Quote]
这是编译器决定的,例如你用
short s1;
short s2;
object s3 = s1+ s2;
s3 is int
崔鹏飞 2008-08-26
  • 打赏
  • 举报
回复
s2 s3都是short,两个short相加结果为什么是int?
freewind0521 2008-08-26
  • 打赏
  • 举报
回复
s1 + s2 在运算时会转化为int类型在计算,所以结果也是int类型
崔鹏飞 2008-08-26
  • 打赏
  • 举报
回复
好的,非常感谢
我姓区不姓区 2008-08-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cuipengfei1 的回复:]
s2 s3都是short,两个short相加结果为什么是int?
[/Quote]
很简单,比如

short a = short.MaxValue;
short b = short.MaxValue;
//a+b就会越界
ericzhangbo1982111 2008-08-26
  • 打赏
  • 举报
回复
考试题吧。。。。
貌似在那里见到过

rangeon 2008-08-26
  • 打赏
  • 举报
回复
[Quote=引自MSDN:]
存在从 short 到 int、long、float、double 或 decimal 的预定义隐式转换。

不能将存储大小更大的非文本数值类型隐式转换为 short(有关整型的存储大小的信息,请参见 整型表(C# 参考))。例如,请看以下两个 short 变量 x 和 y:

short x = 5, y = 12;


以下赋值语句将产生一个编译错误,原因是赋值运算符右侧的算术表达式在默认情况下的计算结果为 int 类型。

short z = x + y; // Error: no conversion from int to short

若要解决此问题,请使用强制转换:

short z = ( short )(x + y); // OK: explicit conversion
[/Quote]
jietuan 2008-08-26
  • 打赏
  • 举报
回复
编译器决定的,没有办法,我觉得尽量少用short类型
相关推荐

109,339

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者