public static int setNibble(int num, int nibble, int which) {
return num & ~(0xF << (which * 4)) | (nibble << (which * 4));
}
Here:
- the
& ~(0xF << (which * 4))
masks out the original value of the nibble; - the
| (nibble << (which * 4))
sets it to the new value.