Is a logical right shift by a power of 2 faster in AVR?

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

For example, is

myUnsigned >> 4

any faster than

myUnsigned >> 3

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.

Let's look at the datasheet:

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.

2b. BASIC MATH - AVR ASM INTRODUCTION, If we need to divide by a power of two such as 2,4,8 etc. The result can be achieved by shifting bits to the right. Each shift to the right is a division by two. The� It is frequently stated that arithmetic right shifts are equivalent to division by a (positive, integral) power of the radix (e.g., a division by a power of 2 for binary numbers), and hence that division by a power of the radix can be optimized by implementing it as an arithmetic right shift. (A shifter is much simpler than a divider.

In the AVR instruction set, arithmetic shift right and left happen one bit at a time. So, for this particular microcontroller, shifting >> n means the compiler actually makes n many individual asr ops, and I guess >>3 is one faster than >>4.

This makes the AVR fairly unsual, by the way.

Bit Shifting (left shift, right shift), A bit shift moves each digit in a number's binary representation left or right. For positive numbers, a single logical right shift divides a number by 2, throwing out any remainders. No prior computer science training necessary—we'll get you up to speed 228 Park Ave S #82632, New York, NY US 10003 (862) 294-2956. C defines a special operation for dealing with bits, and it is represented by two less than symbols in a row << or two greater than symbols in a row >>. This is called a bit shift. The value on the left side of the << is shifted a number times equal to the value on the right hand side of the <<.

You have to consult the documentation of your processor for this information. Even for a given instruction set, there may be different costs depending on the model. On a really small processor, shifting by one could conceivably be faster than by other values, for instance (it is the case for rotation instructions on some IA32 processors, but that's only because this instruction is so rarely produced by compilers).

According to all logical shifts are done in one cycle for the ATMega328. But of course, as pointed out in the comments, all logical shifts are by one bit. So the cost of a shift by n is n cycles in n instructions.

Division only with bitwise shifts, As an example of a multiplication using bit shifts is on the fact that 10 = 8 + 2, and that 8 and 2 are both powers of 2 - so achievable by shifts. the data is from the ADC in the AVR, so i guess it would be unsigned. what about the value of Z,C,N or V in the SREG after i do a logical right shift to 0x00 or 0x01? the main reason for this question is because i'm using the ADC and getting 2 bytes of data back (hi and low).

It depends on how the processor is built. If the processor has a barrel-rotate it can shift any number of bits in one operation, but that takes chip space and power budget. The most economical hardware would just be able to rotate right by one, with options regarding the wrap-around bit. Next would be one that could rotate by one either left or right. I can imagine a structure that would have a 1-shifter, 2-shifter, 4-shifter, etc. in which case 4 might be faster than 3.

Microcontrollers & bit-level operations, In this article I will focus on basic bit-level operations: AND (&), OR (|), XOR (^), NOT It also helps to make the program smaller, much faster and allows to Bitwise binary AND does the logical AND of the bits in each position of a Note that a bitwise shift-right will be the equivalent of integer division by 2. If we need to multiply by a power of two such as 2,4,8 etc. The result can be achived by shifting bits to the left. Each shift to the left is a multiplication by two. The Logical Shift Left (LSL) command is used on the lower byte because it will shift the contents one bit to the left, a zero is shifted into the lowest bit and the highest bit is

Disassemble first then time the code. Dont be discouraged by people telling you, you are wasting your time. The knowledge you gain will put you in a position to be the goto person for putting out the big company fires. The number of people with real behind the curtain knowledge is dropping at an alarming rate in this industry.

Sounds like others explained the real answer here, which disassembly would have shown, single bit shift instruction. So 4 shifts will take 133% of the time that 3 shifts took, or 3 shifts is 75% of the time of 4 shifts depending on how you compared the numbers. And your measurements should reflect that difference, if they dont I would continue with this experiment until you completely understand the execution times.

Shift Operators, This is done to perform a sign extend when working with signed binary values. Power of a 2 Integer Divide vs. Shift Right. If you are dividing by a power of 2, it is � SHIFT RIGHT (>>) The shift right operator is the equivalent of moving all the bits of a specified number of places to the right. Note that a bitwise shift-right will be the equivalent of integer division by 2. For instance, working with a two 8-bit operands and SHIFT RIGHT operator:

Binary to fixed decimal numbers in AVR Assembler, Binary conversion to fixed decimals in AVR-Assembler language, Introduction for By dividing with number that are a power of 2, the AVR feels very comfortable and performs very fast. If no, one bit of the input number is shifted out of the register to the right, into the This instruction is named Logical-Shight-Right or LSR. I like the work around, but I can not believe C18 compiler uses logical instead of arithmetic right-shift for a "signed" variable. I believe this is a compiler bug and should be fixed. "signed" and "unsigned" types exist for a reason. I have used many other processors and C compilers, this is the first time I am dealing with such simple issue.

Arithmetic shift, In computer programming, an arithmetic shift is a shift An arithmetic right-shift by 1 (or 2, 3, …, 7) yields 1111 power of the radix (e.g., a division by a power of 2 for On most processors, shift instructions will execute faster than division instructions. In computer science, a logical shift is a bitwise operation that shifts all the bits of its operand. The two base variants are the logical left shift and the logical right shift. This is further modulated by the number of bit positions a given value shall be shifted, such as shift left by 1 or shift right by n.

[PDF] Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit , developers get an efficient code on either size or speed. However, good coding for an 8-bit AVR helps the developers to know where to focus in improving code efficiency. Table 3-2. Example of different data types and sizes. Unsigned int (16-bit). Unsigned main loop, the device is put in power save mode. The speed� This can be replaced with a left shift and an xor if the shift is wider than the number of bits you want to add, easy example is (i<<1)^1, which adds one to a doubled value. This does not of course apply to a right shift (power of two divide) because only a left (little endian) shift fills the gap with zeros.

  • Why don't you measure yourself?
  • Who cares if x >> 4 is faster than x >> 3? They have different semantics, so it does not matter which one is faster. Anyway, I have never encountered an architecture where the right operand of a bit shift operator had any performance impact.
  • @FredOverflow: On the ATMega, the bit-shift instruction doesn't take a "number of bits to shift" operand. Regarding x >> 4 versus x >> 3 -- maybe the OP has some liberties here (e.g. doing fixed-point arithmetic and has a certain amount of latitude in how large the fractional component is)
  • Division is famous for being extremely expensive (roughly 40 cycles on a modern desktop processor that can do several shifts in one cycle, more than that because it is implemented in software to add insult to injury on an embedded chip).
  • @FredOverflow: I'd guess he's trying to implement some kind of fixed-point integer. In that case, he can switch between x >> 4 and x >> 3, just be changing the number of fraction bits, as long as the precision/range are still good enough for the application.
  • Thank you! I had to replace a floating point number with an integer, but this had to be multiplied larger in order to keep precision. I am trying to find an ideal coefficient so that I spend the least possible time crunching the int back down to the unmultiplied size.
  • it's not unusual. Most (if not all) 8-bit microcontrollers don't have a barrel shifter and must shift one bit at a time
  • 8086 and 80286 don't have a barrel shifter either, so the farther the shift distance the slower it gets
  • Beware: The shift instructions always shifts by only one bit... so the further you shift, the longer it takes.
  • @Martin B Thanks for pointing out, I should have noticed it, the information was available in the same PDF.