3

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;
}

4 Answers4

7

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().

Community
  • 1
  • 1
David Heffernan
  • 587,191
  • 41
  • 1,025
  • 1,442
4

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.

maxdefolsch
  • 169
  • 5
1

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.

Wojtek Surowka
  • 19,646
  • 4
  • 44
  • 49
0
^ 

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

jaimin
  • 563
  • 7
  • 25