Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 这个reverseBytes方法是如何工作的?_C_Byte_Bit_Bit Shift - Fatal编程技术网

C 这个reverseBytes方法是如何工作的?

C 这个reverseBytes方法是如何工作的?,c,byte,bit,bit-shift,C,Byte,Bit,Bit Shift,我正在网上查看此功能,并想知道它是如何工作的: /* * reverseBytes - reverse bytes * Example: reverseBytes(0x12345678) = 0x78563412 * Legal ops: ! ~ & ^ | + << >> */ int reverseBytes(int x) { int newbyte0 = (x >> 2

我正在网上查看此功能,并想知道它是如何工作的:

   /*
    * 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;
    }
/*
*反向字节-反向字节
*示例:反向字节(0x12345678)=0x78563412
*法律行动:!~&^ |+>
*/
整数反向字节(整数x)
{
int newbyte0=(x>>24)&0xff;
intnewbyte1=(x>>8)&0xff00;

int newbyte2=(x假设对于32位系统,您已将
0x12345678
传递给函数

 int newbyte0 = (x >> 24) & 0xff;     //will be 0x00000012
 int newbyte1 = (x >> 8) & 0xff00;    //will be 0x00003400
 int newbyte2 = (x << 8) & 0xff0000;  //will be 0x00560000
 int newbyte3 = x << 24;              //will be 0x78000000

return newbyte0 | newbyte1 | newbyte2 | newbyte3; will be 0x78563412
intnewbyte0=(x>>24)&0xff;//将是0x00000012
int newbyte1=(x>>8)&0xff00;//将是0x00003400

int newbyte2=(x此函数只需将一个整数中的字节右移,然后将其全部或全部移到一起。 例如,x是0xAABBCCDD: 对于第一个字节,我们将所有字节向右移位,因此我们有0x00000000AA&0xFF,即0xAA

对于第二个字节,我们有0x00AABBCC&0xFF00,即0x0000BB00

等等。
我们只需将位移到正确的位置并擦除所有其他位。

是的,您正确理解代码,但它当然假定int为32位值

    int newbyte0 = (x >> 24) & 0xff; // Shift the bits 24~31 to 0~7
    int newbyte1 = (x >> 8) & 0xff00; // Shift the bits 16~23 to 8~15
    int newbyte2 = (x << 8) & 0xff0000; // Shifts bit bits 8~15 to 16~23
    int newbyte3 = x << 24; // Shift bits 0~7 to 24~31

    return newbyte0 | newbyte1 | newbyte2 | newbyte3; // Join all the bits
int newbyte0=(x>>24)&0xff;//将位24~31移到0~7
int newbyte1=(x>>8)&0xff00;//将位16~23移位到8~15
int newbyte2=(x
将数字24位向右移位,使最左边的字节现在成为最右边的字节。然后它使用掩码(
0xff
)将其余字节归零,这是多余的,因为移位将使它们归零,因此可以省略掩码

int newbyte1 = (x >> 8) & 0xff00;
将数字8位右移,使左侧的第二个字节现在成为右侧的第二个字节,其余字节用掩码清零

int newbyte2 = (x << 8) & 0xff0000;
最后,您只需
所有字节即可完成反转


实际上,您可以使用
printf(“%x”,newbyte)
打印每个字节,在代码中一步一步地执行此过程。
%x
格式允许您以十六进制打印。

代码假定32位整数和8位字节。32位整数由4个字节组成: 假设这4个字节在内存中的布局如下:

+---------------------------------+
|Byte 4 | Byte 3 | Byte 2 | Byte 1|
+---------------------------------+
这可能与给定CPU类型的类型有关。在解释由多个字节组成的整数时,某些CPU系列会将最左边的字节(具有较低内存地址的字节)视为整数的最高有效字节-此类CPU称为big-endian。其他CPU则会做相反的操作,它们将处理整数中最右边的字节一个整数,具有最大内存地址的字节作为最高有效字节-小尾端CPU。因此,您的函数将整数从一个尾端转换为另一个尾端

int newbyte0 = (x >> 24) & 0xff;
这将获取上面描述的整数(4个字节),将其向右移动24位,并屏蔽除低8位以外的所有内容,新字节0现在看起来是这样的,其中字节4是
x
的原始字节4,其他3个字节的所有位都设置为零

+---------------------------------+
|  0    |  0    |   0    | Byte 4 |
+---------------------------------+
相似地

 int newbyte1 = (x >> 8) & 0xff00;
将第8位向右移位,并从左侧屏蔽除2.字节中的8位以外的所有内容。结果如下所示,原始值
x

+---------------------------------+
|  0    |  0    |  Byte 3 |   0   |
+---------------------------------+
最左边的两个字节的处理方式类似,只需将
x
左移即可完成相同的任务

终于有了

 newbyte0 | newbyte1 | newbyte2 | newbyte3;
它组合了您在上面创建的所有整数,每个整数只剩下原始
x
的8位。按位执行
,最终得到

+---------------------------------+
|Byte 1 | Byte 2 | Byte 3 | Byte 4|
+---------------------------------+
+---------------------------------+
|  0    |  0    |  Byte 3 |   0   |
+---------------------------------+
 newbyte0 | newbyte1 | newbyte2 | newbyte3;
+---------------------------------+
|Byte 1 | Byte 2 | Byte 3 | Byte 4|
+---------------------------------+