[Mono-list] Bitwise operation weirdness

Miquel Ramírez Miquel Ramírez
Tue, 25 Jan 2005 14:30:47 +0100


Thank you for your involved answer. It just seemed weird to me that an
operation on two variables typed as 'short' were returning an 'int'.
But it seems to be a feature, not a bug :)

Miguel.


On Tue, 25 Jan 2005 14:30:22 +0200, Mike Welham <mike@digitalnova.co.za> wrote:
> Hi Miguel,
> 
> > bitwise.cs(11) error CS0029: Cannot convert implicitly from `int' to
> > `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
> > variables results in an 'int' (32-bit integer number, I suppose).
> 
> The compiler is implicitly converting the two r-values to ints prior to d=
oing 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
> > the bitwise or operation down to a short, but I can't help feeling
> > 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 opera=
tors, overload resolution (§14.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
> 
> <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
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>