交换两个十六进制数的位置

来源:互联网 发布:数据库前置库怎么配置 编辑:程序博客网 时间:2024/06/13 20:58
#include <stdio.h>
#include <stdlib.h>


unsigned replace_byte(unsigned x, int i, unsigned char b)
{
    unsigned temp=x;
    unsigned long  temp1 = (i+1)*8;//左移位数
    int t1,t2;
    t1 = (((x>>temp1)<<8)+(unsigned)b)<<(temp1-8);//左移
    t2 = temp<<((4-i)*8)>>((4-i)*8);//右移
    return t1|t2;
}


int main()
{
    unsigned s;
    s=replace_byte(0x12345678,2,0xAB);
    printf("%x\n",&s);
    s=replace_byte(0x12345678,0,0xAB);
    printf("%x\n",&s);
    return 0;
}