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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:07:54+00:00 2026-05-17T22:07:54+00:00

I have a quick question, suppose I have the following code and it’s repeated

  • 0

I have a quick question, suppose I have the following code and it’s repeated in a simliar way 10 times for example.

if blah then
    number = number + 2^n
end if

Would it be faster to evaluate:

number = number + blah*2^n?

Which also brings the question, can you multiply a boolean value times a integer (Although I am not sure the type that is returned from 2^n, is it an integer or unsigned..etc)? (I’m working in Ada, but let’s try to generalize this maybe?)

EDIT: Sorry I should clarify I am looking at 2 to the power of n, and I put c in there cause I was interested for my own learning in the future if I ever run into this problem in c and I think there are more c programmers out there on these boards then Ada (I’m assuming and you know what that means), however my current problem is in the Ada language, but the question should be fairly language independent (I hope).

  • 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-17T22:07:55+00:00Added an answer on May 17, 2026 at 10:07 pm

    if we are talking about C and blah is not within your control, then just do this:

    if(blah) number += (1<<n);
    

    There is really not a boolean in C and does not need to be, false is zero and true is not zero, so you cannot assume that not zero is 1 which is what you would need for your solution, nor can you assume that any particular bit in blah is set, for example:

    number += (blah&1)<<n;
    

    Would not necessarily work either because 0x2 or 0x4 or anything non-zero with bit zero clear is considered a true. Typically you will find 0xFFF…FFFF (minus one, or all ones) used as true, but you cannot rely on typical.

    Now, if you are in complete control over the value in blah, and keep it strictly to a 0 for false and 1 for true then you could do what you were asking about:

    number += blah<<n;
    

    And avoid the potential for a branch, extra cache line fill, etc.

    Back to the generic case though, taking this generic solution:

    unsigned int fun ( int blah, unsigned int n, unsigned int number )
    {
        if(blah) number += (1<<n);
        return(number);
    }
    

    And compiling for the two most popular/used platforms:

        testl   %edi, %edi
        movl    %edx, %eax
        je  .L2
        movl    $1, %edx
        movl    %esi, %ecx
        sall    %cl, %edx
        addl    %edx, %eax
    .L2:
    

    The above uses a conditional branch.

    The one below uses conditional execution, no branch, no pipeline flush, is deterministic.

      cmp   r0,#0
      movne r3,#1
      addne r2,r2,r3,asl r1
      mov   r0,r2
      bx    lr
    

    Could have saved the mov r0,r2 instruction by re-arranging the arguments in the function call, but that is academic, you wouldnt burn a function call on this normally.

    EDIT:

    As suggested:

    unsigned int fun ( int blah, unsigned int n, unsigned int number )
    {
        number += ((blah!=0)&1)<<n;
        return(number);
    }
    
      subs  r0, r0, #0
      movne r0, #1
      add   r0, r2, r0, asl r1
      bx    lr
    

    Certainly cheaper, and the code looks good, but I wouldnt make assumptions that the result of blah!=0, which is zero or whatever the compiler has defined as true always has the lsbit set. It doesnt have to have that bit set for the compiler to generate working code. Perhaps the standards dictate the specific value for true. by re-arranging the function parameters the if(blah) number +=… will also result in three single clock instructions and not have assumptions.

    EDIT2:

    Looking at what I understand to be the C99 standard:

    The == (equal to) and != (not equal
    to) operators are analogous to the
    relational operators except for their
    lower precedence. Each of the
    operators yields 1 if the specified
    relation is true and 0 if it is false.

    Which explains why the above edit works and why you get the movne r0,#1 and not some other random number.

    The poster was asking the question with regards to C but also noted that ADA was the current language, from a language independent perspective you should not assume “features” like the C feature above and use an if(blah) number = number + (1<<n). But this was asked with a C tag so the generically (processor independent) fastest result for C is, I think, number += (blah!=0)<<n; So Steven Wright’s comment had it right and he should get credit for this.

    The posters assumption is also basically correct, if you can get blah into a 0 or 1 form then using it in the math is faster in the sense that there is no branch. Getting it into that form without it being more expensive than a branch is the trick.

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

Sidebar

Related Questions

Just a quick question... I currently have the following jQuery code with a selector
Group, I have a quick question? -Is there a way to trigger this onclick
I have a quick question about fetching results from a weakly typed cursor and
just a quick question: I am a CS undergrad and have only had experience
just a quick question, if I have a matrix has n rows and m
Quick question. What do you think, I have a few sites that use a
Suppose I have a class structure like the following, where I have a temporary
I have another quick question about SQLAlchemy. If I have added a query_property [1]
I have a quick question. I'm matching class names of LI elements to turn
I have a quick question regarding the scale effect... I have a crude animation

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.