Error when trying to perform a bitwise not (~) on a UInt16 in C#

后端 未结 3 1831
醉酒成梦
醉酒成梦 2021-01-26 05:52

For some reason, I am simply not understanding (or seeing) why this works:

UInt32 a = 0x000000FF;
a &= ~(UInt32)0x00000001;

but this does n

3条回答
  •  深忆病人
    2021-01-26 06:04

    Your expression evaluated at compile time and as result overflow error is detected and blocking compilation.

    Sample below show that run-time does not throw exception by default:

    UInt16 a = 0x00FF;
    UInt16 b = 0x0001;
    a &= (UInt16)~b; 
    

    Note that your code also was converting int (as result of ~ operation on UInt16 converted to int) - so I moved ~ before cast.

    Why: C# compiler generally tries to prevent unintentional code behavior if possible, which combined with all bit-wise operations defined on int/uint/long/ulong and compile time constant evaluation leads to this compile time error.

提交回复
热议问题