我必须使用 java 为整数设置半字节。老实说,我对如何将半字节转换/设置/更改为我想要的半字节感到困惑。我的 TA 告诉我它应该是大约 5 行代码,但我什至不知道如何启动它。任何帮助是极大的赞赏。
/* Examples:
* setNibble(0xAAA5, 0x1, 0); // => 0xAAA1
* setNibble(0x56B2, 0xF, 3); // => 0xF6B2
*
* @param num The int that will be modified.
* @param nibble The nibble to insert into the integer.
* @param which Selects which nibble to modify - 0 for least-significant nibble.
*
* @return The modified int.
*/
public static int setNibble(int num, int nibble, int which) {
// i know I will be using bit-wise operators but do not know how to use
// them in this situation
return 0;
}