Looking for a specific bit shift function

Sorry for my extreemeshly lame question but my it ain't giving me a rest

I was looking for a function that does that.

Lets' say I have a byte

byte BYTE1=0b00000001;

and I want to shift that byte to the left, so I shift it one position at a time...

0b00000010;
0b00000100;
0b00001000;
0b00010000;
0b00100000;
0b01000000;
0b10000000;

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.

N = (byte)(N << 1) | (N >> 7);   // rotate left
N = (byte)(N >> 1) | (N << 7);  // rotate right
N = (N<<1)||((N&&0b10000000)?1:0)

You need a single '|' for this to work, otherwise the expression will be evaluted as a boolean operation.

(byte)(N >> 7);

The Atmega does not have a shift by n instruction, so although perfectly legal in C/C++, it is not very efficient.

Looking at the assembler generated for

N = (byte)(N << 1) | (N >> 7);

i get six instructions: (value originally in r24)

        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.

This code I couldn't understand
"N = (N<<1)||((N&&0b10000000)?1:0)" even with a singe |
What the hell this means ?1:0 :o

so is this legit?
N = (byte)(N << 1) | (N >> 7);

Does such a function that I am looking for exist?
:-/

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.

What is the most efficient way to code a rotate?

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.

mov r25,r24
rol r25
rol r24

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.