return x;
}
/*
* byteNot - bit-inversion to byte n from word x
* Bytes numbered from 0 (LSB) to 3 (MSB)
* Examples: getByteNot(0x12345678,1) = 0x1234A978
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int byteNot(int x, int n) {
//x 第 n 个字节每位都和 1 异或实现取反
int y = 0x@;
n = n<<3;
y = y<<n;
x = (x^y);
return x;
}
/*
* byteXor - compare the nth byte of x and y, if it is same, return 0,
if not, return 1
* example: byteXor(0x12345678, 0x87654321, 1) = 1
* byteXor(0x12345678, 0x87344321, 2) = 0
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 2
*/
int byteXor(int x, int y, int n) {
//把 x 和 y 的第 n 个字节取出来异或,再转换为逻辑的 0 和 1
n = n<<3;
x = x>>n;
y = y>>n;
2
评论0