Rotate left

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Rotate left

Post by Jan Lichtenbelt »

Is there a C-code for rotate left and rotate right?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Rotate left

Post by Benj »

Hi Jan,

By rotate do you mean shift or do you need the bits to wrap around?

Here is some code to shift.

Code: Select all

var = var >> 1;
If you want to shift and wrap then you will likely have to do something like this.

Code: Select all

count = 0;
while (count < shiftcount)
{
bitsave = var & 0x01;
var = var >> 1;

if (bitsave == 0x01)
 var = var | 0x80;

count = count + 1;
}
Or to go the other way.

Code: Select all

count = 0;
while (count < shiftcount)
{
bitsave = var & 0x80;
var = var << 1;

if (bitsave == 0x80)
 var = var | 0x01;

count = count + 1;
}

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Rotate left

Post by Jan Lichtenbelt »

I think the sortest way to rotate left of the variable Ab will be:

asm MOVF gbl_FCV_AB, F
asm RLF gbl_FCV_AB, F

and rotate right:
asm MOVF gbl_FCV_AB, F
asm RRF gbl_FCV_AB, F

Post Reply