设置四位为一个int - java的 [英] Setting the nibble for an int - java
问题描述
我必须设置四位使用Java的整数。老实说,我对如何转向/设置/更改四位以我想要的四位困惑。我的TA告诉我,它应该是约5 code线,但我不知道如何甚至开始吧。任何帮助是极大的AP preciated。
/ *例子:
* setNibble(0xAAA5,为0x1,0); // => 0xAAA1
* setNibble(0x56B2,0xF的,3); // => 0xF6B2
*
* @参数NUM将被修改的int。
* @参数轻咬插入到整数的蚕食。
* @参数用于选择哪个蚕食修改 - 0最低显著四位。
*
返回:修改后的INT。
* /
公共静态INT setNibble(INT NUM,诠释轻咬,诠释它){
//我知道我将使用位运算符,但不知道怎么用
//他们在这情况
返回0;
}
首先,你需要屏蔽掉你想设置(使用按位的和轻咬的&安培;
来做到这一点)。
不是设置四位(移动到想要的位置),使用按位的或的 |
来设置四位
由于这看起来像功课,我不会张贴code。
I have to set the nibble for an integer using java. Honestly I am confused on how to shift/set/change a nibble to an nibble that I want. My TA told me it is supposed to be about 5 lines of code but I don't know how to even start it. Any help is greatly appreciated.
/* 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;
}
First you need to mask out the nibble you want to set (use bitwise and &
to do this).
Than set the nibble (shifted to the wanted position) and use bitwise or |
to set the nibble.
Since this looks like homework, I will not post the code.
这篇关于设置四位为一个int - java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!