c# - Bitshifting Int16 仅适用于文字

标签 c# bit-shift short

当我在 VS 中编写此代码时,它不起作用(“无法将‘int’隐式转换为‘short’。存在显式转换。您是否缺少强制转换?”):

short A = 5;
short B = 1 << A;

但是这段代码绝对没问题:

short A = 1 << 5;

我知道我可以通过将整个表达式转换为 short 来消除错误,但谁能告诉我为什么会这样?

最佳答案

因为 A 不是文字,编译器不知道结果 可以表示为short。因此它需要一个显式的转换。对于字面量 5,编译器认为结果是 32,这可以放入 short 中。

关于c# - Bitshifting Int16 仅适用于文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12983636/

相关文章:

Java:使用short代替int

java - 短对象创建

java - 类型转换值(value)和由此产生的数学

c# - EpiServer:将字符串转换为 XHTMLString

c# - Microsoft JScript 运行时错误 : Object doesn't support this property or method

c# - 为什么会不断调用 ICommunicationListener.Abort()?

c# - 验证属性从未使用 Moq 设置

C# 反转位移位

c - 将unsigned char字节打包成c中的unsigned int

javascript - 在javascript中反向按位移位