error in power

I wrote this simple code:
1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream>
using namespace std;
int main()  {

    int i=0,j;
         j=0;
        i=2^j;
              cout<<i<<",";
        return 0;
}



and the result is :

2,


where is the problem?!
thanks
^ is the bitwise XOR operator.

1
2
3
4
5
6
int main()
{
    int i = 7; //  00000111
    int j = 154; //10011010
    cout << i^j; //10011101
}


it checks both of the numbers and in the result it puts 1 if there is 1 in one of the numbers :P. sorry for bad english.

if you want power use std::pow(); function
Last edited on
As Breadman said, ^ is known as the XOR operator. it stands for Exclusive Bitwise OR operator.
It will consider the characters in binary form (well it considers everything in binary).
It will check the corresponding bits.
It will return 1 if the two digits are different, and return 0 if they are same.
So
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0
0 ^ 0 = 0

It can be used for encryption.

If you want to calculate the Exponent:

The cmath header file has the pow() function (in the standard namespace).

It will take in two parameters, the first will be the Base and the second an Exponent.

So if you want to calculate 2^0:
pow(2.0, 0)


You will get further info here:
http://www.cplusplus.com/reference/clibrary/cmath/pow/
thanks a lot.
Topic archived. No new replies allowed.