The math power operator is not working properly

short sho1, sho2;
printf("Enter two shorts.\n");
scanf("%hd %hd", &sho1, &sho2);
printf("%hd^%hd is %hd.\n", sho1, sho2, sho1^sho2);

When I enter "2 2", I get this output:

2 ^ 2 is 0.

How did it happen? I use the MinGW GCC compiler in Eclipse, if that matters.

+3
source share
6 answers

^is not a mathematical power operator in C, it is a bitwise exclusive-OR operator . You probably need a pow function that takes two doubles.

+18
source

You are not using the operator that you think you are using.

^- bitwise operator XOR .

You are looking for pow function .

Prototype: double pow (double b, double p);

: math.h(C) cmath (++)

: b p .

, C ++.

+7

^ ; XOR. pow, math.h. :

pow(2.0, 2.0)

4.0 ( , ).

, pow double, %g:

printf("%hd^%hd is %g.\n", sho1, sho2, pow((double)sho1, (double)sho2));
+3

C - ^ "", XOR ( )

+3

You can use the pow () function in C.

pow(base, exponent);

This is in the math.h header file.

+2
source

In c ^, is exclusive or operator. You must use a pow  function to make permissions .

pow(2,2)

+1
source

Source: https://habr.com/ru/post/1727773/


All Articles