移位

#include <iostream>
#include <cstdlib>
using namespace std;

unsigned int num;
unsigned int n;
void left()
{
for (int i = 0; i < n; ++i)
{
//if ((1 << 31) & num) (num <<= 1) |= 1;
//else num <<= 1;
if ((1 << 31) & num) (num = num << 1) |= 1;
else num = num << 1;
}
}

void right()
{
for (int i = 0; i < n; ++i)
{
//if (1 & num) (num >>= 1) |= (1 << 31);
//else num >>= 1;
if (1 & num) (num = num >> 1) |= (1 << 31);
else num = num >> 1;
}
}
void enter()
{
char buf[12];
cout << endl;
cout << "输入原始数字num:";
cin >> num;
cout << "输入要移的位n:";
cin >> n;
_itoa(num, buf, 2);
printf("原始数字 num = %s\n", buf);
cout << endl;
}

int main()
{
char command = ' ';
char buf[12];
while (1)
{
cout << "输入指令<1 左移/ 2 右移/ 3 退出>:";
cin >> command;
if (command == '1')
{
enter();
left();
}
else if(command == '2')
{
enter();
right();
}
else if(command == '3')break;
_itoa(num, buf, 2);
printf("移位后 num = %s\n", buf);
}
return 0;
}

posted @   MoonXu  阅读(163)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
历史上的今天:
2019-04-08 tensorflow 语音识别报错
点击右上角即可分享
微信分享提示