【问题标题】:Why does xor'ing two ushort values not return a ushort? [duplicate]为什么异或两个 ushort 值不返回 ushort? [复制]
【发布时间】:2012-10-10 11:44:07
【问题描述】:

可能重复:
C# XOR on two byte variables will not compile without a cast

为什么在此 C# 代码中出现编译错误?

void test()
{
   ushort a = 0;
   ushort b = 0;
   ushort c = a ^ b; //ERROR
}

错误 CS0266:无法将类型“int”隐式转换为“ushort”。存在显式转换(您是否缺少演员表?)

【问题讨论】:

    标签: c# implicit-conversion xor ushort


    【解决方案1】:

    看看这个:C# XOR on two byte variables will not compile without a cast

    基本上,使用位运算符,任何小于 int 的操作数都会自动转换为 int。

    【讨论】:

    • @dbattaglia - 所以你明白了反对票的原因。这很容易成为评论。
    猜你喜欢
    • 2012-04-21
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多