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

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

Rotate left

Postby Jan Lichtenbelt » Thu Nov 12, 2015 5:04 pm

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

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

Re: Rotate left

Postby Benj » Thu Nov 12, 2015 5:23 pm

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: 794
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 261 times
Contact:

Re: Rotate left

Postby Jan Lichtenbelt » Thu Nov 12, 2015 8:18 pm

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