计算机内部的循环移位

 

#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;

}

你可能感兴趣的:(计算机内部的循环移位)