You are here:

# C++/exponents

Question
I am trying to work with Dev-C++.
Are there exponents in C++?
How do I get 2 to the 5th power to equal 32?
I tried r5 = 2 ^ 5 and the result was 7.

In advance thank you very much.

C and C++ have no operator for computing x to the power y. It _does_ have operators to perform various logical operations on the bits of values - ^ is the bitwise exclusive or (or XOR) operator which has the following truth table:

A    B     A XOR B
0    0        0
0    1        1
1    0        1
1    1        0

So in the case of 2 ^ 5 we have in binary 010 XOR 101 for each bit of the two values yielding 111 or 7 decimal.

The C++ standard library provides a pow function to return x to the power y, having various overloads for various types, mostly floating point types (double, float and long double), although the second exponent parameter is an int in some overloads. You should include the header <cmath>:

double x(2.0);
int y(5);
double x_to_the_y( pow( x, y ) );

In the specific case of powers of 2 you can use the shift left operator:

1 << 5

Would yield 32 by shifting the 1 in the least significant bit 5 places left:

Binary

000001

becomes

100000

Or 32 decimal.
Hope this helps.

C++

Volunteer

#### Ralph McArdell

##### Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

##### Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials