user3149497
user3149497

Reputation: 67

Left shift on an unsigned short without overflow into an integer

if I've a ushort variable which is 0xFFAA and I'll left shift this with 8 bits, I'll get an integer not an unsigned short, why? Here a picture to make you more clear, what I mean:

enter image description here

Is there a way to get from this left shift a 16 bit variable without casting the int into an unsigned short?

Upvotes: 1

Views: 998

Answers (1)

samgak
samgak

Reputation: 24427

If you want to make sure that only the lower 16 bits of a variable are set, you can mask with 0xFFFF using logical AND:

var supposed = (test << 8) & 0xFFFF;

Upvotes: 1

Related Questions