How to involute?

So I'm fairly new to C++ and I need some help. I need to involute (I think that's how this action is called in English) the variable a by the variable b like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
  using namespace std;
  
  int main(){

  int a, b;
  a = 3;
  b = 4;

  a = a ^ b; //So basically a = a * a * a * a;

  cout << a;

  return 0;
  }


This code prints me a 0 instead of 81 as it should. What am I doing wrong? Typing in #include <cmath> didn't help aswell so that's not the case. Could someone help me out here? Thanks in advance! :)
closed account (E0p9LyTq)
The caret (^) at line 10 is the bitwise exclusive OR operator.

http://www.cplusplus.com/doc/tutorial/operators/
closed account (E0p9LyTq)
There are powers functions in the C library, but they only work on floating point variables. You can use them, with type casts.

Here's one possible solution for an integer powers function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

int ipow(int, int);

int main()
{
   int b = 3;
   int e = 4;

   std::cout << ipow(b, e) << "\n";
}


int ipow(int base, int exp)
{
   int result = 1;
   
   while (exp)
   {
      if (exp & 1)
      {
         result *= base;
      }
      exp >>= 1;
      base *= base;
   }

   return result;
}
Last edited on
Okay, just tried it out and it works. Now I understand everything until the while cycle. Could you explain how the code of it works?
well, there's a much easier way to do this function. Simple for loop.

1
2
3
4
5
6
7
int power(int base, int exp){
int result = 1;
for (int i=1; i<=exp; i++){
result*=base;
}
return result;
}


This one is simple. For example 2 to the nth power, is 1 multiplied n times by 2. The for loop does this exact thing.
Last edited on
closed account (E0p9LyTq)
@jgg2002, you forgot to check for an exponent of zero, and I forgot to check for negative exponents:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int ipow(int base, int exp)
{
   if (exp < 0)
   {
      return -1; // only positive exponents calculated
   }

   if (exp == 0)
   {
      return 1;
   }

   int num = base;

   for (int i = 1; i < exp; i++)
   {
      base *= num;
   }
   return base;
}


If it was zero, the for loop would never happen, so 1 would be returned. As for negative, I forgot.
[UPDATED CODE]

1
2
3
4
5
6
7
8
9
10
int power(int base, int exp){
int result = 1;
if (exp<0){
    return -1;
}
for (int i=1; i<=exp; i++){
result*=base;
}
return result;
}
closed account (E0p9LyTq)
We are doing the same thing with an exponent of zero, I simply prefer to make it explicit what is returned with a zero exponent.

This shows code that returns the same result can be written more than one way, and each way is not wrong.
I never said it was wrong. The first version you posted was a bit more complex than I think should be, so I posted my version. And yes, the second way you posted is the same thing I posted.
Topic archived. No new replies allowed.