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


this question has answer here:

why following raise compile time error: 'cannot implicitly convert type 'int' 'byte':

        byte = 25;         byte b = 60;          byte c = ^ b; 

this make sense if using arithmentic operator because result of + b larger can stored in single byte.

however applying xor operator pointless. xor here bitwise operation can never overflow byte.

using cast around both operands works:

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

i can't give rationale, can tell why compiler has behavior stand point of rules compiler has follow (which might not you're interesting in knowing).

from old copy of c# spec (i should download newer version), emphasis added:

14.2.6.2 binary numeric promotions clause informative.

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

  • if either operand of type decimal, other operand converted type decimal, or compile-time error occurs if other operand of type float or double.
  • otherwise, if either operand of type double, other operand converted type double.
  • otherwise, if either operand of type float, other operand converted type float.
  • otherwise, if either operand of type ulong, other operand converted type ulong, or compile-time error occurs if other operand of type sbyte, short, int, or long.
  • otherwise, if either operand of type long, other operand converted type long.
  • otherwise, if either operand of type uint , other operand of type sbyte, short, or int, both operands converted type long.
  • otherwise, if either operand of type uint, other operand converted type uint.
  • otherwise, both operands converted type int.

so, operands smaller int converted int these operators (and result int non-relational ops).

i said couldn't give rationale; however, make guess @ 1 - think designers of c# wanted make sure operations might lose information if narrowed need have narrowing operation made explicit programmer in form of cast. example:

byte = 200; byte b = 100;  byte c = + b;  // value truncated 

while kind of truncation wouldn't happen when performing xor operation between 2 byte operands, think language designers didn't want have more complex set of rules operations need explicit casts , other not.


just small note: above quote 'informational' not 'normative', covers cases in easy read form. strictly speaking (in normative sense), reason ^ operator behaves way because closest overload operator when dealing byte operands (from 14.10.1 "integer logical operators"):

int operator ^(int x, int y);  

therefore, informative text explains, operands promoted int , int result produced.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -