问题描述:
对下面代码中的a进行位操作(只能进行位运算),实现输出OPQ
int a = 0x434241;
printf(“%s\n”, (char*)&a);
参考代码1:(使用异或操作)
#include <stdio.h>
int main()
{//异或(^)的方法,最后结果为OPQ对于0x51504f,把原来的数0x434241异或上某个数得到,根据异或的性质,相同数异或得0,不同数异或得1
int a = 0x434241;
int b = 0x12120e;
int c = a^b;
printf("%s\n", (char*)&c);
return 0;
}
参考代码2:(使用与(&)和或(|)相结合的方法)
#include <stdio.h>
int main()
{ //与(&)和或(|)相结合的方法,最后结果为OPQ对于0x51504f,把原来的数0x434241先与上某个数,
//可以让把那些和结果不同的1全部变成0,然后第二步使用或操作,能把结果不同的0全部变成1,这样结果就是我们预期的了
int a = 0x434241;
int b = 0xFDFDFF;
int c = 0x10100E;
int d = (a & b) | c;
printf("%s\n", (char*)&d);
return 0;
}