Java从int var获取第一个和最后一个2字节

Dan*_*ilo 5 java bit-manipulation bit-shift

我想将一个int转换为2个字节,表示该int.

可能必须使用按位和位移,但我不知道该怎么做.

int x; /* val to convert */


// ?????????


int b12; /* the first 2 bytes */
int b34; /* the last  2 bytes */
Run Code Online (Sandbox Code Playgroud)

Pau*_*aul 10

// Shift 16 bits to the right.
int b12 = x >>> 16;

// Zeroes the upper 16 bits.
int b34 = x & 0xffff;
Run Code Online (Sandbox Code Playgroud)