如何旋转字中的位

发布于 2024-10-02 19:23:40 字数 311 浏览 0 评论 0原文

我正在使用 dsPIC33F 和 GCC。我想将一个字中的位向左或向右旋转一次,如下所示:(

       MSB             LSB
input: 0101 1101 0101 1101
right: 1010 1110 1010 1110
left : 1011 1010 1011 1010

如果不清楚,LSB 移动到 MSB 的位置进行右旋转,反之亦然。)

我的处理器已经有一个右旋转(rrnc, rrc)和旋转左指令(rlnc,rlc),所以我希望编译器能够优化它。如果没有,我可能不得不使用内联汇编。

I'm using a dsPIC33F and GCC. I want to rotate the bits in a word once left or right, like this:

       MSB             LSB
input: 0101 1101 0101 1101
right: 1010 1110 1010 1110
left : 1011 1010 1011 1010

(In case it's not clear, the LSB moves into the MSB's position for the right rotate and vice versa.)

My processor already has a rotate right (rrnc, rrc) and rotate left instruction (rlnc, rlc), so I'm hoping the compiler will optimise this in. If not, I might have to use inline assembly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨落画卷 2024-10-09 19:23:40

您可以将它们写为传统移位的明显组合:

x rol N == x << N | x >> width-N
x ror N == x >> N | x << width-N

其中 width 是您旋转的数字中的位数。

智能编译器可能(我认为会)检测到这种组合并编译为旋转指令。

请注意,它适用于无符号,并且宽度等于您正在处理的机器字中的位数(dsPIC 上的无符号 int 为 16)。

You may write them as obvious combination of conventional shifts:

x rol N == x << N | x >> width-N
x ror N == x >> N | x << width-N

where width is number of bits in number you rotate.

Intelligent compiler may (i think it would be) detect this combination and compile to rotation instruction.

Note it works for unsigned and if width is equal to number of bits in machine word you are dealing on (16 for unsigned int on dsPIC).

红颜悴 2024-10-09 19:23:40

C 中没有循环移位。(参考

内联汇编可能是可行的方法,如果性能至关重要。否则,您可以使用上面链接的文章中的代码。

There is no circular shift in C. (Reference)

Inline assembly might be the way to go, if performance is critical. Otherwise you could use the code in the article linked above.

攒一口袋星星 2024-10-09 19:23:40

有 dsPIC 的 GCC 吗?查看其手册是否具有循环移位的内在功能。另一个选项是内联汇编。

There is a GCC for dsPIC? Look in its manual if it has got an intrinsic for circular shifts. The other option is inline asm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文