Ash Ash - 1 month ago 7
C# Question

C# XOR on two byte variables will not compile without a cast

Why does the following raise a compile time error: 'Cannot implicitly convert type 'int' to 'byte':

byte a = 25;
byte b = 60;

byte c = a ^ b;


This would make sense if I were using an arithmentic operator because the result of a + b could be larger than can be stored in a single byte.

However applying this to the XOR operator is pointless. XOR here it a bitwise operation that can never overflow a byte.

using a cast around both operands works:

byte c = (byte)(a ^ b);

Answer

I can't give you the rationale, but I can tell why the compiler has that behavior from the stand point of the rules the compiler has to follow (which might not really be what you're interesting in knowing).

From an old copy of the C# spec (I should probably download a newer version), emphasis added:

14.2.6.2 Binary numeric promotions This clause is informative.

Binary numeric promotion occurs for the operands of the predefined +, ?, *, /, %, &, |, ^, ==, !=, >, <, >=, and <= binary operators. Binary numeric promotion implicitly converts both operands to a common type which, in case of the non-relational operators, also becomes the result type of the operation. Binary numeric promotion consists of applying the following rules, in the order they appear here:

  • If either operand is of type decimal, the other operand is converted to type decimal, or a compile-time error occurs if the other operand is of type float or double.
  • Otherwise, if either operand is of type double, the other operand is converted to type double.
  • Otherwise, if either operand is of type float, the other operand is converted to type float.
  • Otherwise, if either operand is of type ulong, the other operand is converted to type ulong, or a compile-time error occurs if the other operand is of type sbyte, short, int, or long.
  • Otherwise, if either operand is of type long, the other operand is converted to type long.
  • Otherwise, if either operand is of type uint and the other operand is of type sbyte, short, or int, both operands are converted to type long.
  • Otherwise, if either operand is of type uint, the other operand is converted to type uint.
  • Otherwise, both operands are converted to type int.

So, basically operands smaller than an int will be converted to int for these operators (and the result will be an int for the non-relational ops).

I said that I couldn't give you a rationale; however, I will make a guess at one - I think that the designers of C# wanted to make sure that operations that might lose information if narrowed would need to have that narrowing operation made explicit by the programmer in the form of a cast. For example:

byte a = 200;
byte b = 100;

byte c = a + b;  // value would be truncated

While this kind of truncation wouldn't happen when performing an xor operation between two byte operands, I think that the language designers probably didn't want to have a more complex set of rules where some operations would need explicit casts and other not.


Just a small note: the above quote is 'informational' not 'normative', but it covers all the cases in an easy to read form. Strictly speaking (in a normative sense), the reason the ^ operator behaves this way is because the closest overload for that operator when dealing with byte operands is (from 14.10.1 "Integer logical operators"):

int operator ^(int x, int y); 

Therefore, as the informative text explains, the operands are promoted to int and an int result is produced.

Comments