AVR中逻辑右移2的幂是否更快? [英] Is a logical right shift by a power of 2 faster in AVR?

查看:98
本文介绍了AVR中逻辑右移2的幂是否更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以2的幂进行移位时执行逻辑右移是否更快

I would like to know if performing a logical right shift is faster when shifting by a power of 2

例如,是

myUnsigned >> 4

比任何更快

myUnsigned >> 3

我很高兴每个人的第一反应就是告诉我,不要担心像这样的小事情,它使用正确的算法和集合来削减重要的数量级.我完全同意您的意见,但是我实际上是在尽我所能从嵌入式芯片(ATMega328)中挤出所有东西-我刚刚获得了不愧为"woohoo!"的性能提升.通过用位移代替除法,所以我向你保证这很重要.

I appreciate that everyone's first response will be to tell me that one shouldn't worry about tiny little things like this, it's using correct algorithms and collections to cut orders of magnitude that matters. I fully agree with you, but I am really trying to squeeze all I can out of an embedded chip (an ATMega328) - I just got a performance shift worthy of a 'woohoo!' by replacing a divide with a bit-shift, so I promise you that this does matter.

推荐答案

让我们看一下数据表:

http://atmel.com/dyn/resources/prod_documents/8271S.pdf

据我所知,ASR(算术右移)总是移位一位,并且不能占用位数.执行需要一个周期.因此,右移n位将需要n个周期. 2的幂与任何其他数字相同.

As far as I can see, the ASR (arithmetic shift right) always shifts by one bit and cannot take the number of bits to shift; it takes one cycle to execute. Therefore, shifting right by n bits will take n cycles. Powers of two behave just the same as any other number.

这篇关于AVR中逻辑右移2的幂是否更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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