0

So i am confused on why i am getting this error.

Operator '<<' cannot be applied to operands of type 'int' and 'uint'

Everything is a uint type but it won't accept it.

This is the algorithm:

    public void Test(uint[] arr, uint b)
    {
        for (uint x = 0; x < arr.Length; x++)
        {
            uint reverse = 0;
            for (uint i = 0; i < bits; i++)
            {
                reverse |= (((x & (1 << i)) >> i) & 1) << (b - 1 - i);
            }
            arr[x] = reverse;
        }
    }

Why is this not allowed, what am i doing wrong?

WDUK
  • 1,412
  • 1
  • 12
  • 29

1 Answers1

1

The right side of the operator needs to be an int, as seen in the C# specs

Standard ECMA-334 - 12.10 Shift operators

...

When declaring an overloaded shift operator, the type of the first operand shall always be the class or struct containing the operator declaration, and the type of the second operand shall always be int

Probably easiest to use int, then convert it to uint at the end

for (var x = 0; x < arr.Length; x++)
{
   var reverse = 0;

   for (var i = 0; i < bits; i++)
   {
      reverse |= (((x & (1 << i)) >> i) & 1) << ((int)b - 1 - i);
   }

   arr[x] = (uint)reverse;
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Thanks, yeah i opted for that in the end. Don't understand why its not supported for uint though. Its just binary after all... – WDUK Mar 19 '19 at 06:40