I need to perform a bitwise left shift on a 16-bit integer (ushort / UInt16), but the bitwise operators in C# seem to apply to int (32-bit) only. How can I use << on an ushort, or at least get to the same result with a simple workaround?
+4
A:
Cast the resulting value back into ushort after shifting:
ushort value = 1;
ushort shifted = (ushort)(value << 2);
driis
2010-09-29 07:34:11
will `value` be implicitly converted to `uint` for the `<<` operator?
xtofl
2010-09-29 07:39:18
almost embarrassing, so simply this was :) I did almost the same, but forgot the brackets around (value << 2)
KBoek
2010-09-29 07:41:40
@xtofl, value will be implicitly converted to int.
driis
2010-09-29 07:42:05
@xtofl, `int` as @driis says. Note also that in this case, and many similar cases, the result will be the same either way.
Jon Hanna
2010-09-29 08:36:36