20

Possible Duplicate:
Divide and Get Remainder at the same time?

Is it possible to get both the quotient and the remainder of integer division in a single step, i.e., without performing integer division twice?

Community
  • 1
  • 1
dtech
  • 47,916
  • 17
  • 112
  • 190
  • You mean the "result" of the division? It's called the "quotient". – R. Martinho Fernandes Nov 29 '11 at 21:56
  • well, lets say I call " 25 % 10" which results in 5, cuz 2x10 = 20 and 5 is the remainder, what I want to get is the 2 out of the modulo operation as well, is it possible? – dtech Nov 29 '11 at 21:57
  • Some CPUs and some languages have this capability, see e.g. http://stackoverflow.com/questions/3895081/divide-and-get-remainder-at-the-same-time – Paul R Nov 29 '11 at 21:58
  • 5
    Most compilers (should) optimise something like `int c = a % b; int d = a / b;` into one operation (eg, `div` on x86, which returns both). – Matthew Iselin Nov 29 '11 at 21:59
  • @ddriver, yep, that's the quotient. – R. Martinho Fernandes Nov 29 '11 at 22:00
  • 3
    What do you mean by single step? Are you looking a for single function to call? Are you looking for something that generates a single assembly instruction? Are you looking for something that generates assembly that takes a certain number of clock cycles to execute? – Michael Price Nov 29 '11 at 22:03

2 Answers2

27

div will do this. See reference and example:

/* div example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  div_t divresult;
  divresult = div (38,5);
  printf ("38 div 5 => %d, remainder %d.\n", divresult.quot, divresult.rem);
  return 0;
}

Output:

38 div 5 => 7, remainder 3.

EDIT:

The C Specification says:

7.20 General utilities

The types declared are size_t and wchar_t (both described in 7.17),
div_t
which is a structure type that is the type of the value returned by the div function,
ldiv_t
which is a structure type that is the type of the value returned by the ldiv function, and
lldiv_t
which is a structure type that is the type of the value returned by the lldiv function.

... but it doesn't say what the definition of div_t is.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • @ddriver: Yes, but see my edit. – John Dibling Nov 29 '11 at 22:09
  • yes, thank you very much for the detailed response – dtech Nov 29 '11 at 22:11
  • 2
    FYI - The [GLIBC implementation of `div`](http://bit.ly/uqny7z) simply does a divide and a modulo, so you aren't gaining anything. Actually, what you gain is the function call overhead. – Mike Steinert Nov 29 '11 at 22:46
  • 3
    @MikeSteinert: That depends on your architecture - eg glibc has an [assembly implementation of `div` for Alpha](http://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/alpha/div.S;h=d1a724d3758997ed800f995fba5ddf2ae82e148e;hb=febcd83655138bcb01b2680e170e6773a1ec813c) – caf Nov 29 '11 at 23:52
8

Yes, there is a standard function called div() (and ldiv, and maybe even lldiv) that does this.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285