C++使用按位右移/按位左移运算符

1.按位右移运算符(>>)
将数据除以2^n(2的n次方)

2.按位左移运算符(<<)
将数据乘以2^n(2的n次方)

使用按位运算符计算数据

#include

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;


    return 0;

}

效果图
C++使用按位右移/按位左移运算符_第1张图片

转载于:https://www.cnblogs.com/ButterflyEffect/p/6370776.html

你可能感兴趣的:(c/c++)