Why can't i use the operators << and >> like this?
byte num = 32;
byte aux = num << 5;
"Why can't i use the operators << and >> like this?"
That is because your C# expression num<<5 will evaluate to int . The proper way to assign to a byte is to cast the int to a byte, discarding the upper bytes of the int, that is: (byte)(num<<5) . The result after the cast will be a value between 0 and 255 as intended.
Because << will multiply 32 with 2^5 and maximum byte value is 255
byte num = 32;
int aux = num << 5;
this code will be valid instead
Because the shift operators are defined only for the int, uint, long, and ulong types, the result of an operation always contains at least 32 bits. If the left-hand operand is of another integral type (sbyte, byte, short, ushort, or char), its value is converted to the int type, as the following example shows:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.