How come the output is 5 0 I could not understand the logic of 3^6=5,actually it should be 729 right? Here is the code
#include <stdio.h>
int main()
{
int a;
printf("%d\n",(3^6));
printf("%d",(a^a));
return 0;
}
How come the output is 5 0 I could not understand the logic of 3^6=5,actually it should be 729 right? Here is the code
#include <stdio.h>
int main()
{
int a;
printf("%d\n",(3^6));
printf("%d",(a^a));
return 0;
}
The ^ operator is bitwise xor. To understand why 3^6 is 5 let's look at binary representations of those values:
3 = 0011
6 = 0110
and so
3^6 = 0101 = 5
You did not initialise a, which means that a^a is undefined behaviour.
For exponentiation you need to use pow().
The ^ operator in C isn't for exponents : it is the bitwise xor.
00000011 (3)
xor
00000110 (6)
=
00000101 (5)
Use pow() for exponents.
And you didn't initialize a, so be careful with that.
The operator ^ in C is not power - C has no builtin operator for that, only pow function:
double x = pow(3,5);
The operator ^ is bitwise XOR.
^
is a operator used for XORing. include #include<math.h> header file and use pow(3,6) to evaluate power and u'll get 729