Having trouble writing the bit operations to convert an int to a short depending on which 16 bits wanted, ex: 1 will be the left most 16 bits and 0 will be the right most 16. All help is appreciated!
/**
* 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;
}
}
I do not want to use >>> or <<<
This snippet will work correctly with both positive and negative numbers: