Left bit shifting 255 (as a byte)

前端 未结 7 703
臣服心动
臣服心动 2020-12-01 18:41

Can anyone explain why the following doesn\'t compile?

byte b = 255 << 1

The error:

Constant value \'510\' can

相关标签:
7条回答
  • 2020-12-01 18:43
    byte b = 0xff & (255 << 1);
    
    0 讨论(0)
  • 2020-12-01 18:45

    have you tried casting it?

    byte b = (byte)(255 << 1)
    

    This is an interesting approach - the above code will work if wrapped in a unchecked block like this:

    unchecked
    {
        byte b = (byte)(255 << 1);
    }
    

    Since it is unchecked the value is truncated to the intended value of 254. So it is possible to do this with a cast!

    0 讨论(0)
  • 2020-12-01 18:49

    The result of the << operator is an Int32, not what you put into it.

    You need to cast the result of the shift, not the input. Additionally, it will produce an overflow (it is larger than a byte afterall), so you need to specify that you need an unchecked cast.

    In other words, this will work:

    Byte b = unchecked((Byte)(255 << 1));
    
    0 讨论(0)
  • 2020-12-01 18:50

    Numeric literals in C# are int, not byte (and the bit shift will be evaluated by the compiler, hence only the 510 remains). You are therefore trying to assign a value to a byte which does not fit. You can mask with 255:

    byte b = (255 << 1) & 0xFF
    

    to reduce the result to 8 bits again. Unlike Java, C# does not allow overflows to go by undetected. Basically you'd have two sensible options when trying to assign 510 to a byte: Either clamp at the maximum value, then you'd get 255, or throw away the bits that do not fit, in which case you'd get 254.

    You can also use unchecked, as lassevk mentioned:

    byte b = unchecked((byte)(255 << 1));
    
    0 讨论(0)
  • 2020-12-01 18:58

    And since << has a higher precedence than & you can save the brackets:

    byte b = 255 << 1 & 0xff;
    
    0 讨论(0)
  • 2020-12-01 18:59
    255 << 1
    

    will give you more than one byte.

    0 讨论(0)
提交回复
热议问题