微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数组比特位循环左移和右移

下面直接给出代码,详细请看代码


#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]);
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐