In general:
value = (value & ~mask) | (newvalue & mask);
mask
is a value with all bits to be changed (and only them) set to 1 - it would be 0xf in your case. newvalue
is a value that contains the new state of those bits - all other bits are essentially ignored.
This will work for all types for which bitwise operators are supported.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…