+ 2
what about a^b
example #include <iostream> using namespace std; int main() { int x = 5 ^ 6; cout << x; return 0; } why output is 3 ?
18 ответов
+ 5
It is the bitwise xor operator which perform the operation on bit - level of variable i. e. first convert them to binary and then perform the operation on each bit.
^ is bitwise XOR which gives 0 IF BOTH THE BITS ARE EQUAL.
5=101 // in binary
6=110 //in binary
x=011 //in binary which gives 3 as decimal.
[edit] no need to include cmath header file or any other header file.
+ 2
Because in C++ " ^ " isn't defined. But you can find the same thing with"include<math.h>"
+ 1
You've hit upon the C heritage in C++, which tried to give programmers easy access to raw CPU operations in order to allow complete optimisation where necessary (such as in highly utilised loops).
"a ^ b" is "a XOR b", with XOR being the logical operation "exclusive or" which is the equivalent of "(a or b) and not (a and b)".
With Boolean variables, this is easy to understand, but when you apply these Boolean operators to int variables, C (and therefore C++) treat the int variable as an array of Boolean bits used to depict the integer.
5 is represented in Boolean as 0101 (0x2³ + 1x2² + 0x2¹ + 1x2°)
6 is represented in Boolean as 0110 (0x2³ + 1x2² + 1x2¹ + 0x2°)
When you XOR those two arrays you get an array that represents 3:
0101 5
0110 6
-----
0011 3 (0x2³ + 0x2² + 1x2¹ + 1x2°)
Most CPU operation sets include XOR as a simple operation requiring a single processor step, so it's very fast to process, and you can use this numeric shorthand to process a collection of Boolean flags in a single operation. If you have a need to seriously optimise your code, this can be very useful.
+ 1
You need to include both iostream and cmath simultaneously.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
//insert code from before
}
0
i think you are trying to use python in C++ 😜😜😜
thought the answer is :
When you allocated managed memory, that memory can be moved around by the garbage collector. The ^ operator is a pointer for managed memory, that continues to point to the correct place even if the garbage collector moves the object it points to.
#SiD
0
You're trying to get 5 to the 6th power, right? If that's what you're looking for, you should include the cmath header and write "int x = (int) pow(5.0, 6)". The ^ is actually the XOR operator, not an exponent symbol.
0
ty but please try my code, output is 3 , why??
0
its the allocated memory to your synax
I GUESS
#SiD
0
#include <iostream>
using namespace std;
int main()
{
int x = (int) pow(5.0,6);
cout << x;
return 0;
}
Sean .. it doesnt work
0
@SuFi You need to include the <cmath> header for it to work.
0
thank you Myk and Ahmed but i want to say 5^6 = 5*5*5*5*5*5 . and sorry again about my bad English
0
add this at the start for sean's solution
#include <cmath>
0
#include <cmath>
using namespace std;
int main()
{
int x = (int) pow(5.0,6);
cout << x;
return 0;
}
doesn't work :(
0
thank you Sean <3
0
@sean curry
Cmath is a header file for function like pow, trigonometric function and all but for bitwise operator there is no need to use that.
0
a self-multiply the number of b
0
nc ^^
- 1
this XOR
This operation is performed between two bits (a and b). The result is 1 if either one of the two bits is 1, but not in the case that both are. There for, if neither or both of them are equal to 1 the result is 0.
0011
1010
====
1001