Changing specific set of bits in a byte
问题 I am working on a function that receives a byte and needs to change some of the bits in that byte. For example, the function receives: 11001011 Then I need to set the MSB to 0, its easy enough: buffer[0] &= ~(1 << 7); But then I need to set bits 6 through 3 (I refer LSB as bit 0 here) to an argument that gets supplied to the function. This argument can be an integer from 0 to 6 . The important thing is I should not change any other bits. I tried with masking and stuff but I failed miserably.