【问题标题】:I don't know what does this mean (x^y) ? in c++我不知道这是什么意思 (x^y) ?在 C++ 中
【发布时间】:2017-07-01 12:10:08
【问题描述】:

这个函数是干什么用的?并且它不是用于幂函数。

    #include<iostream>

    using namespace std;

    int main(){

    int x,y;

    cout<<(x^y)<<endl;/* this is the unkown (X^Y)*/

    return 0;

    }

【问题讨论】:

标签: c++


【解决方案1】:

^ 运算符是按位的XOR。以6和12为例

6 二进制是:110

二进制的12是:1100

xor 遵循以下规则:“第一个或第二个但不是两者”。这是什么意思?它的真值表应该说清楚:

A     B     A^B
0     0      0
0     1      1
1     0      1
1     1      0

您可以看到,唯一的1-bits 是设置了 or A 或 B(但不是两者)的那些。

回到第一个例子:

A    1100 => 12
B    0110 => 6
A^B  1010 => 10

【讨论】:

    【解决方案2】:

    这是异或。如果您想了解更多信息,请参阅此处https://en.wikipedia.org/wiki/Exclusive_or

    【讨论】:

      【解决方案3】:

      c++中的幂函数是

      #include <math.h>
      #include <iostream>
      int main()
      {
          int x, y;
          std::cout << "Give numbers " << std::endl;
          std::cout << "x = ";
          std::cin >> x;
          std::cout << "y = ";
          std::cin >> y;
          std::cout << "Result = " << pow(x, y) << std::endl;
          return 0;
      
      }
      

      您的版本是 XOR(逻辑运算),用于例如嵌入式系统等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多