Page 1 of 1

Rotate left

Posted: Thu Nov 12, 2015 5:04 pm
by Jan Lichtenbelt
Is there a C-code for rotate left and rotate right?

Re: Rotate left

Posted: Thu Nov 12, 2015 5:23 pm
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;
}

Re: Rotate left

Posted: Thu Nov 12, 2015 8:18 pm
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