and this is the spot where I ask my question, is there a specific function that does that the next time the byte is shifted this '1' is not lost ,but returned
to the begining of the byte:
Is there a specific bit shift function for that:
0b00000001;
and is there again such a specific function that shifts the byte to the right,so when it shifts,the rightmost(LSB) bit becomes
the leftmost(MSB).
0b00000001;//Before the shift
0b10000000;//After the shift
Ah, yes, there might be a Rotate rather than a Shift instruction in the machine instruction set (which you can call with the asm keyword ... dont have the details)
Meanwhile in the highlevel language we can fake it
N = (N<<1)||((N&&0b10000000)?1:0)
I'm sure we can make it even more tense, but this is off the top of my head and will get you started.
mov r25,r24
lsl r25
rol r24
clr r24
rol r24
or r25,r24
I do not find a direct rotate-instruction in the instruction set. Instead, rol will copy the MSB to the Carry flag and feed the old carry flag into LSB. At least, there is no shift-down that will be run 7 times in a loop.
The rotate left and rotate right I posted should work.
I've done 6502, 68000 and 8086 assembler before but I'm not familiar with all the details of AVR assembler. What is the most efficient way to code a rotate?
What the hell this means ?1:0
a = b ? 1 : 0;
is a shorter way of saying
if (b)
a = 1;
else
a = 0;
and has the advantage that the variable assigned to (a) doesn't need to be mentioned twice and that it can form a single statement.
I did not look at the assembly and so your version seems quite efficient after all (saved by the optimizer). A "x << n" where both "x" and "n" are variables however typically compile to a loop with n-iterations.
The six assembly instructions could at least be replaced by the 3 above (i.e rol:ing a copy in a scrap register first to get the correct bit in the carry flag and then doing rotate-left-through-carry on the proper register.
I surmise it can be done in two cycles also, but the cpu datasheet doesn't say exactly how the various flags are affected by a compare instruction.