如何在一个字旋转位 [英] How to rotate the bits in a word

查看:229
本文介绍了如何在一个字旋转位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是dsPIC33F和海湾合作委员会。我想旋转一个字位一次左或右,像这样:

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

(如果它是不明确的,LSB移动到最高位的位置为右旋转,反之亦然。)

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

我的处理器已经有右移(rrnc,RRC),并向左旋转指令(rlnc,RLC),所以我希望编译器将在此进行优化,如果不是,我可能要使用内联汇编。

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.

推荐答案

您可以写他们的常规班次为显而易见的组合:

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

其中,宽度为您旋转数的位数。

智能编译器(我认​​为这将是)检测到这种组合编译旋转指令。

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

请注意它适用于无符号,如果宽度等于机器字的位数你(有关的dsPIC unsigned int类型16)上的处理。

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).

这篇关于如何在一个字旋转位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆