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 8586541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:20:03+00:00 2026-06-11T22:20:03+00:00

if I do: int x = 4; pow(2, x); Is that really that much

  • 0

if I do:

int x = 4;
pow(2, x);

Is that really that much less efficient than just doing:

1 << 4

?

  • 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-06-11T22:20:04+00:00Added an answer on June 11, 2026 at 10:20 pm

    Yes. An easy way to show this is to compile the following two functions that do the same thing and then look at the disassembly.

    #include <stdint.h>
    #include <math.h>
    
    uint32_t foo1(uint32_t shftAmt) {
        return pow(2, shftAmt);
    }
    
    uint32_t foo2(uint32_t shftAmt) {
        return (1 << shftAmt);
    }
    

    cc -arch armv7 -O3 -S -o - shift.c (I happen to find ARM asm easier to read but if you want x86 just remove the arch flag)

        _foo1:
    @ BB#0:
        push    {r7, lr}
        vmov    s0, r0
        mov r7, sp
        vcvt.f64.u32    d16, s0
        vmov    r0, r1, d16
        blx _exp2
        vmov    d16, r0, r1
        vcvt.u32.f64    s0, d16
        vmov    r0, s0
        pop {r7, pc}
    
    _foo2:
    @ BB#0:
        movs    r1, #1
        lsl.w   r0, r1, r0
        bx  lr
    

    You can see foo2 only takes 2 instructions vs foo1 which takes several instructions. It has to move the data to the FP HW registers (vmov), convert the integer to a float (vcvt.f64.u32) call the exp function and then convert the answer back to an uint (vcvt.u32.f64) and move it from the FP HW back to the GP registers.

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

Sidebar

Related Questions

I haven't really used variance calculation that much, and I don't know quite what
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,
Atm i have sth like that: template<int n> struct Pow { enum{val= Pow<n-1>::val<<1}; };
From http://msdn.microsoft.com/en-us/library/system.math.pow.aspx int value = 2; for (int power = 0; power <= 32;
I'm working on a Function that will do a POW long handed. It's an
I have two methods: power and factorial: public static long pow(int x, int n)
public String starString(int n){ int m = (int)Math.pow(2,n); String str=; str = starString(m-1,str); return
I am giving input as int and according to that input I want combination
long a = (long)Math.pow(2, 32); // a = 4294967296 :) long a = (int)(long)Math.pow(2,

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.