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

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


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

相关文章

财联社10月10日讯(编辑 赵昊)当地时间周二(10月8日),美...
PANews 9月29日消息,币安前首席执行官赵长鹏在X平台发文称:...
凤凰网科技讯 3月11日,比特币报价突破71000美元,创历史新高...
赵长鹏 凤凰网科技讯 北京时间9月28日,据彭博社报道,美国当...
“前华人首富”赵长鹏的出狱时间或再提前。 Binance(币安)...
财联社5月24日讯(编辑 史正丞)当地时间周四盘后,根据一份...