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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:02:08+00:00 2026-06-18T05:02:08+00:00

Recently, I started using expressions like the following: res += (i + n /

  • 0

Recently, I started using expressions like the following:

res += (i + n / i) * !(n % i);

Where I assume, that !(n % i) will always have a value of 1 or 0, and thus can be used in calculations directly, instead of writing lengthy if-statements like

if(!(n % i))
    res += (i + n / i);

In case you wonder, these lines are taken from a function I wrote to calculate the sum of proper divisors of a number n:

unsigned int sum_of_divisors(unsigned int n)
{
    unsigned int res = 1;

    unsigned int i;
    for(i = 2; i < sqrt(n); ++i)
            res += (i + n / i) * !(n % i);
    res += i * (i * i == n);

    return res;
}

My questions are, is this code guaranteed to behave the way I intended it to behave? What is the approximate performance impact of this (multiplication vs. conditional jump)? would the compiler do that anyway, if appropriate?

EDIT: Please note that I’m not particularly concerned about the performance of the actual code. I’d just like to know, out of pure professional interest, which one of the two would perform better, and why, and also how the compiler would handle each case.

As for the reason why I wrote it that way, it works well with my brain 🙂

It’s hard to describe, but I’ve got a better feeling with multiplying 1 or 0 instead of ternary operators or if statements, at least in some cases.

Thanks,
Andy

  • 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-18T05:02:09+00:00Added an answer on June 18, 2026 at 5:02 am

    Yes, the compiler is guaranteed to behave the way you expect.

    No, it will not make your code faster, unless the compiler is really low-quality. The compiler should treat both versions of the code roughly equivalently and choose the way it thinks is best to do the conditional logic.

    By the way, in principle, the ! operator is a conditional branch. Some implementations (cpu archs) may have ways to optimize it not to need a real program-counter branch, but the same methods will work for most conditionals.

    Note that there may be one way your code is “better” from the optimizing standpoint. In writing:

    res += (i + n / i) * !(n % i);
    

    you have given the compiler permission to write to res in both code paths. In the form:

    if(!(n % i))
        res += (i + n / i);
    

    the compiler can only write to res if the condition is true. If res is local and its address has not leaked, the compiler can determine that it’s safe to perform excess writes anyway, but if the address of res is visible outside the function, the compiler must assume other threads may be able to access it and that code paths that don’t modify res in the abstract machine must not modify it in the generated code (since they might not hold the lock necessary to modify it safely).

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

Sidebar

Related Questions

I recently started using WPF and the MVVM framework, one thing that I have
I've recently started using new C++11 features like lambda expressions to make my code
Recently I have found and I have started using in my preprocessor the following
I recently started using Mercurial and like the way I have VCS protection for
I recently started using Masterpages, the thing is I would like to add text
I recently started using NERDTree with vim, and have set it to automatically open
I recently started using AutoFixture library (http://autofixture.codeplex.com/) for Unit Testing and I quite like
So I have recently started using jQuery, jQueryUi and ASP.NET MVC. And I have
I recently started using Eclipse IDE and have read at a number of places
I have recently started using a shared-host for my clients so see project progress

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.