c++将函数左旋转或右旋转


//将16位数字左旋转或右旋转
//original:0x2345
//rotate left 0x5234
//rotate right 0x3452

#include <iostream>
using namespace std;
unsigned short int rotate16Left(unsigned short int num, int n) {
	return (num >> n | num << (16 - n));
	
}
unsigned short int rotate16Right(unsigned short int num, int n) {
	return (num << n | num >> (16 - n));
}
int main()
{
	unsigned short int num = 0x2345;
	printf("original:%#x\n", num);
	printf("rotated left:%#x\n", rotate16Left(num,4));
	printf("rotated right:%#x\n", rotate16Right(num,4));

	return 0;
}


posted @ 2020-05-14 19:32  淇实是我  阅读(337)  评论(0编辑  收藏  举报