what about a^b | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 ?

8th Jan 2017, 12:31 PM
SuFi
18 Answers
+ 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.
8th Jan 2017, 1:18 PM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 2
Because in C++ " ^ " isn't defined. But you can find the same thing with"include<math.h>"
8th Jan 2017, 3:53 PM
Matin
Matin - avatar
+ 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.
8th Jan 2017, 1:16 PM
Myk Dowling
Myk Dowling - avatar
+ 1
You need to include both iostream and cmath simultaneously. #include <iostream> #include <cmath> using namespace std; int main(){ //insert code from before }
8th Jan 2017, 1:39 PM
DaemonThread
DaemonThread - avatar
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
8th Jan 2017, 12:52 PM
Siddharth Naithani
Siddharth Naithani - avatar
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.
8th Jan 2017, 12:56 PM
DaemonThread
DaemonThread - avatar
0
ty but please try my code, output is 3 , why??
8th Jan 2017, 12:58 PM
SuFi
0
its the allocated memory to your synax I GUESS #SiD
8th Jan 2017, 1:00 PM
Siddharth Naithani
Siddharth Naithani - avatar
0
#include <iostream> using namespace std; int main() { int x = (int) pow(5.0,6); cout << x; return 0; } Sean .. it doesnt work
8th Jan 2017, 1:06 PM
SuFi
0
@SuFi You need to include the <cmath> header for it to work.
8th Jan 2017, 1:09 PM
DaemonThread
DaemonThread - avatar
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
8th Jan 2017, 1:21 PM
SuFi
0
add this at the start for sean's solution #include <cmath>
8th Jan 2017, 1:27 PM
Myk Dowling
Myk Dowling - avatar
0
#include <cmath> using namespace std; int main() { int x = (int) pow(5.0,6); cout << x; return 0; } doesn't work :(
8th Jan 2017, 1:31 PM
SuFi
0
thank you Sean <3
8th Jan 2017, 1:42 PM
SuFi
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.
8th Jan 2017, 1:59 PM
Rishabh Agrawal
Rishabh Agrawal - avatar
0
a self-multiply the number of b
9th Jan 2017, 11:56 AM
Arshiya Kashaf
Arshiya Kashaf - avatar
0
nc ^^
1st Feb 2017, 1:35 PM
Rolando G. Panganiban Jr.
Rolando G. Panganiban Jr. - avatar
- 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
8th Jan 2017, 12:51 PM
ASNM
ASNM - avatar