我一直在寻找这个功能在网上,我想知道它是如何工作的:这个reverseBytes方法是如何工作的?
/*
* reverseBytes - reverse bytes
* Example: reverseBytes(0x12345678) = 0x78563412
* Legal ops: ! ~ &^| + << >>
*/
int reverseBytes(int x)
{
int newbyte0 = (x >> 24) & 0xff;
int newbyte1 = (x >> 8) & 0xff00;
int newbyte2 = (x << 8) & 0xff0000;
int newbyte3 = x << 24;
return newbyte0 | newbyte1 | newbyte2 | newbyte3;
}
这里就是我想我明白:
0xff
,0xff00
和0xff0000
二进制是1111 1111
,1111 1111 0000 0000
,和1111 1111 0000 0000 0000 0000
分别为- 该方法用掩码创建四个新字节(
0xff
等),然后使用将它们的值相加在一起运营商
我真的不明白这是如何颠倒字节虽然。我希望有详细的解释。谢谢!