C++ 左移运算符和右移运算符的使用
<< 左移运算符的使用: 2 << 2 。表示2的2进制左移两位.乘以2的2次方.2 x 2的2次幂,结果为8
>> 右移运算符的使用:2 >> 1 。 表示2的2进制右移一位,除以2的1次方,除以2的1次幂.结果为1
以下是代码的测试:
#include "iostream"
class Eat
{
public:
int TestLeft() {
int x = 2 << 2;
std::cout << "查看左移之后的数值" << "\t" << x << std::endl;
return 0;
}
int TestRight()
{
int x = 2 >> 1;
std::cout << "查看右移之后的数值" << "\t" << x << std::endl;
return 0;
}
private:
class Eat
{
public:
int TestLeft() {
int x = 2 << 2;
std::cout << "查看左移之后的数值" << "\t" << x << std::endl;
return 0;
}
int TestRight()
{
int x = 2 >> 1;
std::cout << "查看右移之后的数值" << "\t" << x << std::endl;
return 0;
}
private:
};
int main()
{
Eat * eat = new Eat();
eat->TestLeft();
std::cout << std::endl;
eat->TestRight();
std::cin.get();
return 0;
}