KBoek
KBoek

Reputation: 5985

C# bitwise shift on ushort (UInt16)

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?

Upvotes: 14

Views: 13707

Answers (1)

driis
driis

Reputation: 164341

Cast the resulting value back into ushort after shifting:

ushort value = 1;
ushort shifted = (ushort)(value << 2);

Upvotes: 19

Related Questions