根据需要的 16 位,编写将 int 转换为 short 的位操作时遇到问题,例如:1 将是最左边的 16 位,0 将是最右边的 16 位。感谢所有帮助!
/**
* Get a short from an int.
*
* Examples:
* getShort(0x56781234, 0); // => 0x1234
* getShort(0xFF254545, 1); // => 0xFF25
*
* @param num The int to get a short from.
* @param which Determines which short gets returned - 0 for least-significant short.
*
* @return A short corresponding to the "which" parameter from num.
*/
public static int getShort(int num, int which)
{
if(which == 1){
return num>>16;
}
else{
return num << 16;
}
}
我不想使用 >>> 或 <<<