none
Shift << operator cannot be applied to uint

    Question

  • int z = (1 << 3); 
    uint p = (1u << 3u);

    The fist line works but the second line generates this error

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

    How could the shift operator not be applied to unsigned integers, that doesn't make sense?

    Saturday, September 25, 2010 3:48 PM

Answers

  • Check out section 14.8 of the C# Language Specification where it states:

    "The predefined shift operators are listed below.

    • Shift left:
    int operator <<(int x, int count);
    uint operator <<(uint x, int count);
    long operator <<(long x, int count);
    ulong operator <<(ulong x, int count);

    The << operator shifts x left by a number of bits computed as described below.
    The high-order bits outside the range of the result type of x are discarded, the remaining bits are shifted
    left, and the low-order empty bit positions are set to zero.


    • Shift right:
    int operator >>(int x, int count);
    uint operator >>(uint x, int count);
    long operator >>(long x, int count);
    ulong operator >>(ulong x, int count);

    The >> operator shifts x right by a number of bits computed as described below.
    When x is of type int or long, the low-order bits of x are discarded, the remaining bits are shifted
    right, and the high-order empty bit positions are set to zero if x is non-negative and set to one if x is
    negative.
    When x is of type uint or ulong, the low-order bits of x are discarded, the remaining bits are shifted
    right, and the high-order empty bit positions are set to zero."

     

    The problem is that your second argument should always be an int.

    ShaneB

    • Edited by Shane_B Saturday, September 25, 2010 4:03 PM formatting
    • Marked as answer by Mattastica Saturday, September 25, 2010 4:09 PM
    Saturday, September 25, 2010 4:03 PM

All replies

  • Check out section 14.8 of the C# Language Specification where it states:

    "The predefined shift operators are listed below.

    • Shift left:
    int operator <<(int x, int count);
    uint operator <<(uint x, int count);
    long operator <<(long x, int count);
    ulong operator <<(ulong x, int count);

    The << operator shifts x left by a number of bits computed as described below.
    The high-order bits outside the range of the result type of x are discarded, the remaining bits are shifted
    left, and the low-order empty bit positions are set to zero.


    • Shift right:
    int operator >>(int x, int count);
    uint operator >>(uint x, int count);
    long operator >>(long x, int count);
    ulong operator >>(ulong x, int count);

    The >> operator shifts x right by a number of bits computed as described below.
    When x is of type int or long, the low-order bits of x are discarded, the remaining bits are shifted
    right, and the high-order empty bit positions are set to zero if x is non-negative and set to one if x is
    negative.
    When x is of type uint or ulong, the low-order bits of x are discarded, the remaining bits are shifted
    right, and the high-order empty bit positions are set to zero."

     

    The problem is that your second argument should always be an int.

    ShaneB

    • Edited by Shane_B Saturday, September 25, 2010 4:03 PM formatting
    • Marked as answer by Mattastica Saturday, September 25, 2010 4:09 PM
    Saturday, September 25, 2010 4:03 PM
  • Ah, thank you. I'm trying to convert C code to C#, so it looks like I will have to cast the second argument to int.
    Saturday, September 25, 2010 4:09 PM