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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:51:33+00:00 2026-05-23T20:51:33+00:00

In the below code, is there a way to avoid the if statement? s

  • 0

In the below code, is there a way to avoid the if statement?

s = 13;   /*Total size*/
b = 5;    /*Block size*/
x = 0;
b1 = b;
while(x < s)
{
    if(x + b > s)
        b1 = s-x;
    SendData(x, b1);   /*SendData(offset,length);*/
    x += b1;
}

Thanks much!

  • 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-23T20:51:35+00:00Added an answer on May 23, 2026 at 8:51 pm

    You can use a conditional move or branchless integer select to assign b1 without an if-statement:

    // if a >= 0, return x, else y
    // assumes 32-bit processors
    inline int isel( int a, int x, int y ) // inlining is important here
    {
        int mask = a >> 31; // arithmetic shift right, splat out the sign bit
        // mask is 0xFFFFFFFF if (a < 0) and 0x00 otherwise.
        return x + ((y - x) & mask);
    };
    
    // ...
    while(x < s)
    {
        b1 = isel( x + b - s, s-x, b1 );
        SendData(x, b1);   /*SendData(offset,length);*/
        x += b1;
    }
    

    This is only a useful optimization on in-order processors, though. It won’t make any difference on a modern PC’s x86, which has a fast branch and a reorder unit. It might be useful on some embedded systems (like a Playstation), where pipeline latency matters more for performance than instruction count. I’ve used it to shave a few microseconds in tight loops.

    In theory a compiler “should” be able to turn a ternary expression (b = (a > 0 ? x : y)) into a conditional move, but I’ve never met one that did.

    Of course, in a larger sense everyone who says that this is a pointless optimization compared to the cost of SendData() is correct. The difference between a cmov and a branch is about 4 nanoseconds, which is negligible compared to the cost of a network call. Spending your time fixing this branch which happens once per network call is like driving across town to save 1¢ on gasoline.

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

Sidebar

Related Questions

Is there a way to get the code below to return null if no
Normally I use the below code, but is there a better way? lastOfMonth =
in the code below at first if statements block (there will be more than
The below code works when there are a few element in the To list
I have below code to insert a style into DOM (there is a use
For the below code, there ia a form in the location /home/form.php .How can
When running the below code a type is never returned, despite there being a
In the below code instead of specifying the tab name.. Is there anyway we
The code below is resulting in an error on a site in which there
The code below is for a simple newsletter signup widget. I'm sure there's a

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.