Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's that way by design in C#, and, in fact, dates back all the way to C/C++ - the latter also promotes operands to <code>int</code>, you just usually don't notice because <code>int -&gt; char</code> conversion there is implicit, while it's not in C#. This doesn't just apply to <code>|</code> either, but to all arithmetic and bitwise operands - e.g. adding two <code>byte</code>s will give you an <code>int</code> as well. I'll quote the relevant part of the spec here:</p> <blockquote> <p>Binary numeric promotion occurs for the operands of the predefined +, –, *, /, %, &amp;, |, ^, ==, !=, >, &lt;, >=, and &lt;= 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: </p> <ul> <li><p>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.</p></li> <li><p>Otherwise, if either operand is of type double, the other operand is converted to type double. </p></li> <li><p>Otherwise, if either operand is of type float, the other operand is converted to type float.</p></li> <li><p>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.</p></li> <li><p>Otherwise, if either operand is of type long, the other operand is converted to type long.</p></li> <li><p>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.</p></li> <li><p>Otherwise, if either operand is of type uint, the other operand is converted to type uint.</p></li> <li><p>Otherwise, both operands are converted to type int.</p></li> </ul> </blockquote> <p>I don't know the exact rationale for this, but I can think about one. For arithmetic operators especially, it might be a bit surprising for people to get <code>(byte)200 + (byte)100</code> suddenly equal to <code>44</code>, even if it makes some sense when one carefully considers the types involved. On the other hand, <code>int</code> is generally considered a type that's "good enough" for arithmetic on most typical numbers, so by promoting both arguments to <code>int</code>, you get a kind of "just works" behavior for most common cases.</p> <p>As to why this logic was also applied to bitwise operators - I imagine this is so mostly for consistency. It results in a single simple rule that is common for <em>all</em> non-boolean binary types.</p> <p>But this is all mostly guessing. Eric Lippert would probably be the one to ask about the real motives behind this decision for C# at least (though it would be a bit boring if the answer is simply "it's how it's done in C/C++ and Java, and it's a good enough rule as it is, so we saw no reason to change it").</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload