Search code examples
javaintshort

int to short with two most significant bytes


I have an integer

int a = //...

ints in java consist of 4 bytes. If we cast it to short, we will take two least significant bytes. What is the fastest way to get short consisting of 2 most significant bytes of the int?

short b = //take two most significant bytes of a

Solution

  • short b = (short) (a >> 16);