为什么xor'ing两个ushort值不返回ushort?

时间:2012-10-10 11:44:08

标签: c# implicit-conversion xor ushort

  

可能重复:
  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'。存在显式转换(您是否错过了演员?)

1 个答案:

答案 0 :(得分:3)

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

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

相关问题