If I have two bytes a and b, how come:
byte c = a & b;
produces a compiler error about casting byte to int? It does this even if I put an explicit cast in front of a and b.
Also, I know about this question, but I don’t really know how it applies here. This seems like it’s a question of the return type of operator &(byte operand, byte operand2), which the compiler should be able to sort out just like any other operator.
I disagree with always. This works and the result of
a & bis of typelong:The return type is not
intif one or both of the arguments arelong,ulongoruint.The result of
byte & byteis an int because there is no&operator defined on byte. (Source)An
&operator exists forintand there is also an implicit cast frombytetointso when you writebyte1 & byte2this is effectively the same as writing((int)byte1) & ((int)byte2)and the result of this is anint.