[Mono-list] Bitwise operation weirdness
Mike Welham
mike@digitalnova.co.za
Tue, 25 Jan 2005 14:30:22 +0200
Hi Miguel,
> bitwise.cs(11) error CS0029: Cannot convert implicitly from `int' to=20
> `short'
> Compilation failed: 1 error(s), 0 warnings
> If I do not misunderstand the compiler message, it seems that for some =
> reason the result of or-ing together the hiword and the loword=20
> variables results in an 'int' (32-bit integer number, I suppose).
The compiler is implicitly converting the two r-values to ints prior to =
doing to doing the 'or', and then complaining about trying to implicitly =
convert the resultant int to a short.
There is no predefined C# 'or' for shorts, and the best match is the =
'or' for ints. See the C# spec quotes below.
> Why is that? It is easily fixed by explicitly casting the result of=20
> the bitwise or operation down to a short, but I can't help feeling=20
> that this feels weird: is it a C# specification feature?
The C# spec says:
<spec section 14.10>
For an operation of the form x op y, where op is one of the logical =
operators, overload resolution (=A714.2.4) is applied to select a =
specific operator implementation. The operands are converted to the =
parameter types of the selected operator, and the type of the result is =
the return type of the operator.
</spec>
And=20
<spec section 14.10.1>
The predefined integer logical operators are:
int operator &(int x, int y);
uint operator &(uint x, uint y);
long operator &(long x, long y);
ulong operator &(ulong x, ulong y);
int operator |(int x, int y);
uint operator |(uint x, uint y);
long operator |(long x, long y);
ulong operator |(ulong x, ulong y);
int operator ^(int x, int y);
uint operator ^(uint x, uint y);
long operator ^(long x, long y);
ulong operator ^(ulong x, ulong y);
</spec>
Best Regards
Mike