returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving other bits unchanged
问题 my solution get the rightmost n bits of y a = ~(~0 << n) & y clean the n bits of x beginning from p c = ( ~0 << p | ~(~0 << (p-n+1))) & x set the cleaned n bits to the n rightmost bits of y c | (a << (p-n+1)) it is rather long statements. do we have a better one? i.e x = 0 1 1 1 0 1 1 0 1 1 1 0 p = 4 y = 0 1 0 1 1 0 1 0 1 0 n = 3 the 3 rightmost bits of y is 0 1 0 it will replace x from bits 4 to bits 2 which is 1 1 1 回答1: I wrote similar one: unsigned setbits (unsigned x, int p, int n,