[Mono-list] Bitwise operation weirdness

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


Hi,

I was writing some code that performed little endian-big endian
conversion and found the following weirdness on C# ( or Mono)
implementation of bitwise operations. Consider the following code:

using System;


class BitwiseWeirdness
  {
  public static void Main( string[] args )
    {
    short hiword = 0x1000;
    short loword = 0x0010;
    
    short word = hiword | loword;

    if ( word == 0x1010 )
      Console.WriteLine("It works");
    else
      Console.WriteLine("It doesn't");

    }
  };

This does not compile with mono-1.0.4 mcs compiler. The compiler output is:

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).

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?

Miguel Ramírez.