实例十三:左循环移位

方法:result=n<<k|n>>(32-k)

(1) b:将n的左端的k位先放到b中的低位中。
   b=n>>(32-k);
(2) c:将n左移k位,起右边低位k位补0。  
   c=n<<k;
(3)将b和c进行或操作.

 

解释:
原数        1011 1100
右移k位        0000 0010        ----将原数高位的10 移到最低位保存 方法中的b
将原数左移k位   1111 0000        ----将未移动的位全部左移k位。 11 1100 全部左移 方法中的c
进行或操作      1111 0010


代码:

int _tmain(int argc, _TCHAR* argv[])
{
  int n,k,b,c,nResult = 0;

  cout << "请输入原始的值:";
  cin >> n;
  cout << "请输入左移的位:";
  cin>>k;

  b = n>>(32-k) ;     // 将原始数据的高位移动到b的低位中
  c=n<<k;           //把原始数据进行左移
  nResult = b|c;      //或运算,把从前的高位放到移动后的低位中

  cout << endl <<"结果:"<< nResult;

  system("pause");
  return 0;
};

 

相关文章:

  • 2021-09-24
  • 2022-12-23
  • 2022-12-23
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
  • 2019-06-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2021-10-05
相关资源
相似解决方案