#include<iostream>
#include<cmath>
using namespace std;
void f(unsigned char byte)//打印二进制
{
for (int j = 7; j >= 0; j--)
{
if ((1 << j) & byte)
cout << 1;
else
cout << 0;
}
}
void bitBuket(void(*p)(unsigned char), unsigned char byte,int num)//byte需要移位的数,num移位的位数
{
cout << "原来的二进制表示是:";
(*p)(byte);
if (num > 0 && num < 8)
{
for (int i = 0; i < num; i++)
{
if ((1 << 7) & byte)
{
byte <<= 1;
byte |= 1;
}
else
byte <<= 1;
}
}
else if (num<0 && num>-8)
{
unsigned int n = abs(num);
for (int i = 0; i < n; i++)
{
if (1 & byte)
{
byte >>= 1;
byte |= (1 << 7);
}
else
byte >>= 1;
}
}
else if (num >= 8 || num <= -8)
{
cout << "移位后的二进制为:";
(*p)(0);
cout << endl;
}
cout<< "移位后的二进制为:";
(*p)(byte);
cout << endl;
}
int main()
{
do
{
cout << "输入一个数,移位数" << endl;
int i = 0,j=0;
cin >> i >> j;
bitBuket(f, i, j);
} while (cin.get() != 'q');
}