为什么下面的表达式为true或false?没有if语句

时间:2019-06-20 10:42:34

标签: c++ oop

我正在查看一些类,并且已经在c ++中找到了有关以下内容的代码 重载运算符。我知道<<和>>表示移位,但在这种情况下返回值为1。我不明白&运算符的作用,我知道它的意思是“和”,但是没有任何if语句。

#include <iostream>
using namespace std;
class Integer
{
 public:
 int value;
 Integer(int val) : value(val) {}
 int operator ()(int start,int end);
};

int Integer::operator ()(int start,int end)
{
  return (value >> start) & ((1 << (end - start)) - 1);
}

int main()
  {
   Integer n1(122);
   int res = n1(1,3);
   std::cout << n1.value << std::endl;
   }

0 个答案:

没有答案
相关问题