计算机内部的循环移位

 

#include "iostream"

#include "bitset"

#include "limits"

 

using namespace std;

 

#define out2(T) cout<<bitset<numeric_limits<unsigned int>::digits>(T)<<" "

#define SIZE(T) (numeric_limits<unsigned T>::digits)

 

bool SHR31(int x)           //逻辑右移位

{

     return (unsigned int)x>>31;

}

 

/*

     shift 范围0 ~ 31, 超出范围其行为未定义

 */

 

// 循环左移位

int ROL(int x, int n)

{

     return   (x<<n) | ((unsigned int)x >> (32-n));

}

 

// 循环右移位

int ROR(int x, int n)

{

     return ((unsigned int)x>>n) | (x<<(32-n));

}

 

// 带进位循环左移

int RCL(unsigned int x, int n, int &c)

{

     unsigned int d = c;

     c = (!n&&c) + (n && (x>>(31-n)>>1 & 1));

     return x<<n | x>>(31-n)>>2 |  d<<n>>1;

}

 

// 带进位循环右移

int RCR(unsigned int x, int n, int &c)

{

     unsigned int d = c;

     c = (!n&&c) + (n && (x<<1>>n & 1));

     return x>>n | x<<(31-n)<<2 | d<<(31-n)<<1;

}

 

// 算术循环左移

void ARL(int x, int n)

{}

 

// 算术循环右移

void ARR()

{}

 

void test4();

void main()

{

//   test1();

//   test2();

//   test3();

     test4();

}

 

 

void test4()

{

     int x; int n; int c = 0; int d;

    

     x = 1; n = 1;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 1<<29; n = 31;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 2; n = 2;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 0x80000001; n = 16;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

    

     x = 0x80000000; n = 1;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

    

     x = -1; n = 1;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = -1; n = 31;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 0xFFFF0000; n = 16;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 0x7DECABF6; n = 16;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 1; n = 1;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 1<<29; n = 31;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 2; n = 2;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 0x80000001; n = 16;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 0x80000000; n = 1;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

    

     x = -1; n = 1;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = -1; n = 31;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 0xFFFF0000; n = 16;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 0x7DECABF6; n = 16;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

 

     x = 0x7DECABF6; n = 0;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

    

     x = 0x7DECABF6; n = 0;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCR(x, n, c)); cout << c << endl; cout << RCR(x, n, d) << endl << endl;

}

void test3()

{

     int x; int n; int c = 0; int d;

    

     x = 1; n = 1;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 1<<30; n = 1;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 1<<30; n = 2;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 0x80000000; n = 1;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

    

     x = -1; n = 1;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = -1; n = 31;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 0x0000FFFF; n = 16;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 0x7DECABF6; n = 16;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 1; n = 1;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 1<<30; n = 1;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 1<<30; n = 2;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 0x80000000; n = 1;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

    

     x = -1; n = 1;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = -1; n = 31;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 0x0000FFFF; n = 16;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 0x7DECABF6; n = 16;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

 

     x = 0x7DECABF6; n = 0;

     c = 0; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

    

     x = 0x7DECABF6; n = 0;

     c = 1; d = c;

     cout << x << " " << n << " " << c << " "  << endl;

     out2(RCL(x, n, c)); cout << c << endl; cout << RCL(x, n, d) << endl << endl;

}

void test2()

{

     int x, int n;

    

     x = 1; n = 1;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = 1<<31; n = 1;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = 1<<31; n = 2;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = 0x7FFFFFFF; n = -1;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = 0x7FFFFFFF; n = 32;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = 0x7FFFFFFF; n = 47;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = 1; n = -1;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = 1<<30; n = -1;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = 1<<30; n = -2;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = 0x7FFFFFFF; n = -32;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = 0x7FFFFFFF; n = -45;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl<< endl;

 

     x = 0x80000000; n = 1;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = 0x80000000; n = -1;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = 0x80000000; n = 32;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = 0x80000000; n = 33;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = 0x80000000; n = -32;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = 0x80000000; n = -33;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = -1; n = 1;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = -1; n = 31;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = -1; n = 39;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = -1; n = -33;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

    

     x = -1; n = -1;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = 0x0000FFFF; n = 16;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = 0x0000FFFF; n = -16;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = 0xEDB71248; n = 63;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = 0xECB71248; n = -32;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

 

     x = 0; n = -18;

     cout << x << " " << n << " " << endl;

     out2(ROR(x, n)); cout << ROR(x, n) << endl << endl;

}

void test1()

{

     int x, int n;

    

     x = 1; n = 1;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = 1<<30; n = 1;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = 1<<30; n = 2;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = 0x7FFFFFFF; n = -1;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = 0x7FFFFFFF; n = 32;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = 0x7FFFFFFF; n = 47;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = 1; n = -1;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = 1<<31; n = -1;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = 1<<31; n = -2;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = 0x7FFFFFFF; n = -32;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = 0x7FFFFFFF; n = -45;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl<< endl;

 

     x = 0x80000000; n = 1;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = 0x80000000; n = -1;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = 0x80000000; n = 32;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = 0x80000000; n = 33;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = 0x80000000; n = -32;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = 0x80000000; n = -33;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = -1; n = 1;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = -1; n = 31;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = -1; n = 39;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = -1; n = -33;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

    

     x = -1; n = -1;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = 0x0000FFFF; n = 16;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = 0x0000FFFF; n = -16;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = 0xEDB71248; n = 63;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = 0xECB71248; n = -32;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

 

     x = 0; n = -1;

     cout << x << " " << n << " " << endl;

     out2(ROL(x, n)); cout << ROL(x, n) << endl << endl;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值