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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:43:33+00:00 2026-05-25T14:43:33+00:00

You could write something like: int i = 3; int k = 2; int

  • 0

You could write something like:

int i = 3;
int k = 2;
int division = i / k;
int remainder = i % k;

It seems as thought this would, on a low level, ask an ALU to perform two vision operations: one returning the quotient, and one returning the remainder. However, I believe an ALU would most likely calculate both in a single operation. If that is the case, this is not optimally efficient.

Is there a more efficient way of doing it, without asking the CPU to calculate twice? In other words, can it be done in a single operation from C++?

  • 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-25T14:43:33+00:00Added an answer on May 25, 2026 at 2:43 pm

    Actually, the code you wrote won’t generate any division instructions since the compiler can figure out the results at compile time. I wrote a little test program and set the compiler (VC++ 10SP1) to generate an assembly code listing.

    #include <iostream>
    
    using namespace std;
    
    struct result {
        long quotient, remainder;
    };
    
    result divide(long num, long den) {
        result d = { num / den, num % den };
        return d;
    }
    
    int main() {
        result d = divide(3, 2);
        d = divide(10, 3);
        cout << d.quotient << " : " << d.remainder << endl;
        return 0;
    }
    

    I had to write it this way and explicitly tell the compiler to not inline any functions.
    Otherwise the compiler would have happily optimized away most of the code. Here is the resulting assembly code for the divide function.

    ; 8    : result divide(long num, long den) {
    
      00000 55       push    ebp
      00001 8b ec        mov     ebp, esp
    
    ; 9    :     result d = { num / den, num % den };
    
      00003 99       cdq
      00004 f7 7d 08     idiv    DWORD PTR _den$[ebp]
    
    ; 10   :     return d;
    ; 11   : }
    
      00007 5d       pop     ebp
      00008 c3       ret     0
    

    It’s smart enough to generate a single IDIV instruction and use the quotient and remainder generated by it. Modern C and C++ compilers have gotten really good at this sort of optimization. Unless you have a performance problem and have profiled your code to determine where the bottleneck is, don’t try to second guess the compiler.

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

Sidebar

Related Questions

I find this atrocious: std::numeric_limits<int>::max() And really wish I could just write this: int::max
I could write myself a helper class that does this when given a functor,
Sure I could write this myself, but before I go reinventing the wheel is
I would like to write an object generator for a templated RAII class --
I have something like this in my code typedef struct ts_fem_mesh { double **vertices;
If I had a SQL Server table that looks something like this with some
In C++ methods can have optional arguments, like this: void myFunction (int arg1, int
I know I could write scripts and create jobs to run them, but at
I wonder if I could write a program in the C -programming language that
Is there a piece of code I could write to be notified when 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.