Simplezjz
SimpleMLXG
采纳率67.5%
2020-03-08 12:07

左旋转移位(程序填空)求助

已采纳

利用C++语言的移位运算实现左旋转移位。
#include
//val左旋转移n位,1<=n<=7
unsigned char rol(unsigned char val,int n);
//val左旋转移位(rol函数被重载)
unsigned char rol(unsigned char val);
//输出val对应的二进制值
void showBit(unsigned char val);
int main() {
unsigned char x=0xb5;
showBit(x); //输出x对应的二进制值

showBit(rol(x)); //输出x左移1位后对应的二进制值

int n;
std::cin>>n;
showBit(rol(x,n)); //输出x左移n位后对应的二进制值

return 0;

}
unsigned char rol(unsigned char x,int n) {







}
unsigned char rol(unsigned char val) {







}
void showBit(unsigned char val) {







}
输入
1<=正整数<=7
输出
10110101B 01101011B 左移n位后对应的二进制值B

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • caozhy 从今以后生命中的每一秒都属于我爱的人 1年前

    问题解决请点采纳,包括前面几个问题

    unsigned char rol(unsigned char val) {
        return (val % 128) * 2 + val / 128;
    }
    
    unsigned char rol(unsigned char x,int n) {
        for (int i = 0; i < n; i++, x = rol(x));
        return x;
    }
    
    void showBit(unsigned char val, int x = 8) {
        if (x == 0) 
        {
            cout << endl;
            return;
        }
        showBit(val / 2, x - 1);
        cout << val % 2;
    }
    
    点赞 评论 复制链接分享

相关推荐