宿題のためにこのコードを書かなければなりませんが、どこから始めればいいのかさえわかりません。これが私が書かなければならないメソッドへのjavadocです。
/**
* Sets a 4-bit nibble in an int
* Ints are made of eight bytes, numbered like so: 7777 6666 5555 4444 3333 2222 1111 0000
*
* For a graphical representation of this:
* 1 1 1 1 1 1
* 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |Nibble3|Nibble2|Nibble1|Nibble0|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* Examples:
* setNibble(0xAAA5, 0x1, 0) //=> 0xAAA1
* setNibble(0x56B2, 0xF, 3) //=> 0xF6B2
*
* @param num int that will be modified
* @param nibble nibble to insert into the integer
* @param which determines which nibble gets modified - 0 for least significant nibble
*
* @return the modified int
*/
これが私が持っているものです。javadocの最初の例で動作するようになっていますが、これがすべての場合に当てはまるわけではないことはわかっています。特に、int which!= 0;
public static int setNibble(int num, int nibble, int which)
{
int newNibble = (num >> 4);
newNibble = (newNibble << 4) | nibble;
return newNibble;
}
シフトを使用する必要がありますか?このメソッドは1行のコードで実行できると言われました。高度な支援をありがとう!