Tại sao các phép dịch chuyển trên int không dấu cho kết quả chưa được ký, nhưng thao tác trên toán hạng chưa ký kết quả dẫn đến int đã ký?Tại sao hoạt động dịch chuyển luôn dẫn đến int đã ký khi toán hạng là <32 bits
int signedInt = 1;
int shiftedSignedInt = signedInt << 2;
uint unsignedInt = 1;
uint shiftedUnsignedInt = unsignedInt << 2; //OK. unsigned result
short signedShort = 1;
int shiftedsignedShort = signedShort << 2;
ushort unsignedShort = 1;
uint shiftedUnsignedShort = unsignedShort << 2; //CS0266: Can't cast int to uint
sbyte signedByte = 1;
int shiftedSignedByte = signedByte << 2;
byte unsignedByte = 1;
uint shiftedUnsignedByte = unsignedByte << 2; //CS0266: Can't cast int to uint
Cũng thêm 'uint shiftedUnsignedByte = (uint) unsignedByte << 2U;' để có thông tin chi tiết hơn. –