Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pow is 'expensive' #3

Open
hugopeeters opened this issue Jan 7, 2018 · 1 comment
Open

pow is 'expensive' #3

hugopeeters opened this issue Jan 7, 2018 · 1 comment

Comments

@hugopeeters
Copy link
Owner

Suggestion to use custom function:

int myPow2(int n) // returns 2^n, calculated in integer math
{
  int result = 1;
  if (n = 0) return result;
  else if ( n < 0) return -1;
  else if (n > 30) return -1; //overflow
  else
  {
    for (int i = 0; i < n; i++) result = result *2;
    return result;
  }
}
@jawsper
Copy link

jawsper commented Apr 7, 2018

Isn't it the same to use 1 << n for pow(2, n) though? That would be a very cheap operation too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants