//位运算 int insertBits(int N, int M, int i, int j){ for(int start=i;start<=j;start++) N&=~((unsigned int)1<<start); //给i至j位清零 N|=(M<<i); //给i至j位插入 return N; }
int insertBits(int N, int M, int i, int j){ int num=0,count=0; while(N || M) { if (count>=i && count<=j) { num += M%2 * pow(2,count); M >>= 1; } else { num += N%2 * pow(2,count); } N >>= 1; count++; } return num; }