关于左移和右移的口算:
a 左移 n 位就是 a 乘 2 的 n 次方 a 右移 n 位就是 a 除 2 的 n 次方
unsigned int a = 60; // 60 = 0011 1100 c = a << 2; // 240 = 1111 0000 cout << "Line 5 - c 的值是 " << c << endl ; c = a >> 2; // 15 = 0000 1111 cout << "Line 6 - c 的值是 " << c << endl ;<br> c = a << 2; // 240 = 1111 0000 // c = a* 2^2 = a*4 = 60*4 c = a >> 2; // 15 = 0000 1111 // c = a/ 2^2 = a/4 = 60/4 c = a >> n; // c = a * (2^n); c = a << n; // c = a / (2^n);
给出测试代码:
#include <iostream> #include <math.h> using namespace std; int left(int a,int n); int right(int a,int n); int main() { unsigned int a = 33; cout << "关于a左移和右移n位的口算方法" << endl; cout << "a << 2 =" << (a<<2) << endl; cout << "a << 2 =" << (left(a,2)) << endl; cout << "a >> 2 =" << (a>>2) << endl; cout << "a >> 2 =" << (right(a,2)) << endl; return 0; } int left(int a,int n){ return a * pow(2,n); } int right(int a,int n){ return a / pow(2,n); }
运行结果:
关于a左移和右移n位的口算方法 a << 2 =132 a << 2 =132 a >> 2 =8 a >> 2 =8
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.mushiming.com/mjsbk/2561.html