Why use only the lower five bits of the shift operand when shifting a 32-bit value? (e.g. (UInt32)1 << 33 == 2)

后端 未结 3 1812
南笙
南笙 2020-12-17 15:28

Consider the following code:

UInt32 val = 1;
UInt32 shift31 = val << 31;                    // shift31  == 0x80000000
UInt32 shift32 = val << 32;         


        
3条回答
  •  心在旅途
    2020-12-17 16:15

    Unit32 overflows at 32 bits, that is defined in the spec. What are you expecting?

    The CLR does not define a left shift with overflow detection operator(1). If you need that kind of facility you need to check for yourself.

    (1) The C# compiler might cast it to long, but I am not sure.

提交回复
热议问题