Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7017033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:51:44+00:00 2026-05-27T22:51:44+00:00

The power function (written in c++)… long power (long a, long b){ long result=1l;

  • 0

The power function (written in c++)…

long power (long a, long b){
    long result=1l;
    for (int i = 0;i<b;i++){
        result*=a;
    }
    return result;
}

Now I do some output testing…

cout<<power(2l,2l)<<endl;
cout<<power(2l,4l)<<endl;
cout<<power(2l,31l)<<endl;
cout<<power(2l,32l)<<endl;
cout<<power(2l,61l)<<endl;

Output:

4
16
-2147483648
0
0

Well there seems to be some problem with the long falling back to a 32 bit size (instead of staying as a 64 bit). I’m wondering why this doesn’t work, yet if I use the long long type, everything works fine.

Some extra info:

I’m using C++ and the compiler MinGW
I am running a 64-bit OS (Windows 7)

UPDATE:

You guys are awesome! Never thought that this type of thing would be going on.

I just checked some arbitrary PDTs using sizeof and this is what I found…

cout<<sizeof(long)<<" "<<sizeof(int)<<" "<<sizeof(char)<<" "<<sizeof(long long)<<" "<<sizeof(uint64_t)<<endl;

Output:

4 4 1 8 8

So, it looks like my long and int are both 32 bit in size. Some more playing around shows that the intmax_t type is also 64 bit. Practically every single PDT is capped at 64 bits, so if I ever needed to represent a 128-bit integer, does c++ have a built in class for that (something similar to BigInteger in Java)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:51:44+00:00Added an answer on May 27, 2026 at 10:51 pm

    Apparently, the type long is 32 bits in your environment.

    To get around similar problems, I would suggest that you use types like uint64_t instead of relying in the assumption that a native type has a specific size.

    EDIT

    To answer your second question (does c++ have a built in class for 128-bit integers?): No, it does not. Or rather, it does not mandate one. However, if an implementation would provide one you would be able to use something like uint128_t. Personally, I haven’t seen any system that does this, though. There are third party libraries like GMP that provide that functionality, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: The most efficient way to implement an integer based power function pow(int,
Possible Duplicate: The most efficient way to implement an integer based power function pow(int,
I want to write a function that returns the nearest next power of 2
How would I make this Haskell power function tail recursive? turboPower a 0 =
As an exercise I implemented the mathematical power function. Once using recur: (defn power
I have a problem with writing a Cartesian power function. I found many examples
The following function is claimed to evaluate whether a number is integer power of
I have a power function pow that attempts to calculate the value of B
What is wrong with my power function? pow(_,0,1). pow(X,Y,Z) :- pow(X,Y-1,X*Z). ?- pow(2,3,Z). ERROR:
Possible Duplicate: J2ME power(double, double) math function implementation I'm developing a simple j2me application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.