下面直接给出代码,详细请看代码
#include <stdio.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
uint8_t RotateLeft(uint8_t *p,uint8_t len)
{
uint8_t valBit;
if (!len)
return 0;
len--;
valBit = *p >> 7;
do {
*p = (*p << 1) | (*(p + 1) >> 7);
p++;
} while (--len);
*p = (*p << 1) | valBit;
return 1;
}
uint8_t RotateRight(uint8_t *p,uint8_t len)
{
uint8_t valBit;
if (!len)
return 0;
len--;
p += len;
valBit = *p << 7;
do {
*p = (*p >> 1) | (*(p - 1) << 7);
p--;
} while (--len);
*p = (*p >> 1) | valBit;
return 1;
}
int main()
{
uint8_t i = 0;
uint8_t t[3];
t[0] = 0x01;
t[1] = 0;
t[2] = 0;
RotateLeft(t,3);
for (i = 0; i < 3; i++)
{
printf("%d = %02x\r\n",i,t[i]);
}
RotateLeft(t,t[i]);
}
RotateRight(t,t[i]);
}
}