Setting nibble for int java

I need to set nibble to integer using java. To be honest, I am confused as to how to swap / set / change the slice to the half bike I want. My TA told me it should be about 5 lines of code, but I don't know how to start it. Any help is 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;
 }

      

+1


source to share


1 answer


First you need to mask the chickpea you want to install (use bitwise &

for this as well).

Then set the nibble (shifted to the desired position) and use bitwise or |

to set the chunk.



Since this looks like homework, I won't post the code.

+2


source







All Articles