gpt4 book ai didi

c++ - 在一个字节中交换半字节

转载 作者:行者123 更新时间:2023-11-30 05:07:48 24 4
gpt4 key购买 nike

考虑下面交换一个字节中的半字节的代码:

#include <stdio.h>

unsigned char swapNibbles(unsigned char x)
{
return ( (x & 0x0F)<<4 | (x & 0xF0)>>4 );
}

int main()
{
unsigned char x = 100;
printf("%u", swapNibbles(x));
return 0;
}

为什么需要用 0x0F 和 0xF0 进行运算?相反,我可以写

return ((x<<4) | (x>>4));

我用谷歌搜索了一下,人们似乎说它不适用于负数,因为它会用右移的数字填充数字,但是用 0xF0 不会使数字为正数?我错过了什么?

最佳答案

这两个变体是 100% 等效的。 gcc 和 clang 都能够将每个代码片段转换为简单的 rol 指令并将它们编译为相同的汇编代码(当然启用了优化)

unsigned char swapNibbles(unsigned char x)
{
return (x & 0x0F)<<4 | (x & 0xF0)>>4;
}

unsigned char swapNibbles2(unsigned char x)
{
return x << 4 | x >>4 ;
}
swapNibbles(unsigned char):                       # @swapNibbles(unsigned char)
mov eax, edi
rol al, 4
ret
swapNibbles2(unsigned char): # @swapNibbles2(unsigned char)
mov eax, edi
rol al, 4
ret

关于c++ - 在一个字节中交换半字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47114773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com