1. 按位右移运算符 (>>)
将数据除以 2^n(2 的 n 次方)
2. 按位左移运算符 (<<)
将数据乘以 2^n(2 的 n 次方)
使用按位运算符计算数据
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- int main()
- {
- cout << "Enter a number:";
- int Input = 0;
- cin >> Input;
- int Half = Input >> 1;
- int Quarter = Input >> 2;
- int Double = Input << 1;
- int Quadruple = Input << 2;
- cout << "Half:" << Half << endl;
- cout << "Quarter:" << Quarter << endl;
- cout << "Double:" << Double << endl;
- cout << "Quadruple:" << Quadruple << endl;
- system("pause");
- return 0;
- }
效果图
image
来源: http://www.jianshu.com/p/e17e6ae523f9