首先我们需要掩码:掩码是一组“块大小”宽移到右块的“块”。
- 一组“块大小”宽:
(1<<size) - 1
- 一组“块大小”宽移到正确的位置:
((1<<size)-1) << (number*size)
首先,我们清除将被替换的位,然后我们坚持我们想要的位。
- 清除了屏蔽位的旧值:
oldv & ~mask
- 新值转移到正确的位置:
newv << (number*size)
- 新值移动到正确的位置并掩蔽到正确的宽度:
(newv<<(number*size)) & mask
- 清除旧位并插入新值:
(oldv&~mask) | ((newv<<(number*size))&mask)
所以你想要的代码是:
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
一个测试程序:
#include <stdio.h>
void printbits(int n) {
unsigned int i = 1<<(sizeof(n) * 8 - 1);
while (i > 0) {
if (n & i)
printf("1");
else
printf("0");
i >>= 1;
}
}
int main(void) {
int size,number,mask,oldv,newv,result;
size=4;number=2;oldv=0;newv=15;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
size=4;number=2;oldv=~0;newv=0;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
size=3;number=2;oldv=0;newv=7;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
size=3;number=2;oldv=~0;newv=0;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
size=4;number=4;oldv=0xAAAAAAAA;newv=0x15;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
size=5;number=3;oldv=0xAAAAAAAA;newv=0x15;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
return 0;
}
结果:
size = 4
number = 2
oldv = 00000000000000000000000000000000
newv = 00000000000000000000000000001111
mask = 00000000000000000000111100000000
omask = 00000000000000000000000000000000
nmask = 00000000000000000000111100000000
result = 00000000000000000000111100000000
size = 4
number = 2
oldv = 11111111111111111111111111111111
newv = 00000000000000000000000000000000
mask = 00000000000000000000111100000000
omask = 11111111111111111111000011111111
nmask = 00000000000000000000000000000000
result = 11111111111111111111000011111111
size = 3
number = 2
oldv = 00000000000000000000000000000000
newv = 00000000000000000000000000000111
mask = 00000000000000000000000111000000
omask = 00000000000000000000000000000000
nmask = 00000000000000000000000111000000
result = 00000000000000000000000111000000
size = 3
number = 2
oldv = 11111111111111111111111111111111
newv = 00000000000000000000000000000000
mask = 00000000000000000000000111000000
omask = 11111111111111111111111000111111
nmask = 00000000000000000000000000000000
result = 11111111111111111111111000111111
size = 4
number = 4
oldv = 10101010101010101010101010101010
newv = 00000000000000000000000000010101
mask = 00000000000011110000000000000000
omask = 10101010101000001010101010101010
nmask = 00000000000001010000000000000000
result = 10101010101001011010101010101010
size = 5
number = 3
oldv = 10101010101010101010101010101010
newv = 00000000000000000000000000010101
mask = 00000000000011111000000000000000
omask = 10101010101000000010101010101010
nmask = 00000000000010101000000000000000
result = 10101010101010101010101010101010