看题: 0到w/8-1 此时根据输入的参数0x12345678,这是个8位的16进制数.每一位有四个二进制位也就4*8得出 w=32位,w/8-1=3,所以是从0到3编号.是一个倒序,而且是没两个数字编一个号.
编号⬇: 3 2 1 0
意思就是0x12 34 56 78
#include <stdio.h>
unsigned replace_byte(unsigned x,int i,unsigned char b);
int main(void)
{
printf ("0x%X\n",replace_byte(0x12345678,2,0xAB)) ;
printf ("0x%X\n",replace_byte(0x12345678,0,0xAB)) ;
}
unsigned replace_byte(unsigned x,int i,unsigned char b)
{
unsigned char *tar;
tar = (unsigned char *) &x;
tar[i]=b;//你会发现它本来就是小端法输出的,所以参数i可以用作下标.
return x;
}