What is Integer division?

Division of integers that produces an integer quotient. How the fractional part is handled differs between languages: some truncate toward zero (C, Java), and others round down toward negative infinity (Python's //). The results are the same for positive numbers but differ for negative ones.

Japanese name: 整数除算

Integer division appears in 4 practice questions across 3 topics, including Computer Arithmetic, Programming Basics (Pseudocode Tracing), C Programming Basics.

Field: Programming Basics (Pseudocode Tracing)

Differences with negative numbers

Dividing −7 by 2 gives a quotient of −3 and a remainder of −1 in C and Java, which truncate toward zero. Python's //, which rounds down, gives a quotient of −4, and % gives a remainder of 1. In both, "quotient × divisor + remainder = dividend" holds; the remainder takes the sign of the dividend in C and the sign of the divisor in Python. This causes mistakes when programs that handle negative numbers are rewritten in another language.

In pseudocode

Pseudocode questions explain in the question how the fractional part of ÷ is handled. For example, if the question says "the fractional part is discarded when assigning to an integer variable", assigning (lo + hi) ÷ 2 to the integer variable mid drops the 0.5 when lo + hi is odd. This truncation affects results such as the middle position in a binary search.

Relationship with shifts

Integer division by a power of 2 can also be computed with a right shift in binary. For unsigned integers, x divided by 2 is x shifted right by 1 bit. However, an arithmetic right shift of a negative number rounds down, so the result can differ from truncation toward zero.

Try it: questions about Integer division

Under IEEE 754, what result does the standard specify for dividing a positive number by zero?

  1. A0
  2. BThe largest normal number
  3. CPositive infinity
Show answer and explanation

Answer: C. Positive infinity

IEEE 754 specifies that dividing a nonzero number by zero returns positive or negative infinity according to the signs. Infinity is encoded with an all-ones exponent and a zero significand and is also the result of overflow. Computation can continue without stopping, and anomalies can be found by inspecting results later. 0 ÷ 0, which has no defined value, gives NaN. Unlike integer division, floating point does not trap by default.

In C, what are the results of −7 / 2 and −7 >> 1 (with an arithmetic shift) for int?

  1. A−7 / 2 is −3, and −7 >> 1 is −4
  2. BBoth are −4
  3. C−7 / 2 is −4, and −7 >> 1 is −3
Show answer and explanation

Answer: A. −7 / 2 is −3, and −7 >> 1 is −4

C (since C99) defines integer division as truncating toward zero, so −3.5 becomes −3. An arithmetic right shift instead rounds toward negative infinity: shifting −7 (1111 1001) right by one gives 1111 1100 = −4. For negative numbers, shifting and dividing therefore disagree, so when compilers replace division by 2 with a shift they add correction instructions for negatives. For positive numbers the two agree.

FAQ

What happens when dividing by zero?

Integer division by zero is a runtime error (an exception) in many languages and undefined behavior in C. Programs should check that the divisor is not zero before dividing.

How are the quotient and remainder of integer division related?

With quotient q and remainder r, dividend = q × divisor + r. The quotient and remainder are defined as a pair, so if the rounding of the quotient changes (truncation or rounding down), the remainder changes too.

Integer division in the explanations

Questions that mention Integer division (4)

  1. Under IEEE 754, what result does the standard specify for dividing a positive number by zero?
  2. In C, what are the results of −7 / 2 and −7 >> 1 (with an arithmetic shift) for int?
  3. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. What does the following function f return when called as f(9087)?
  4. Which of the following is the output when the code above, which includes a division of integers, is executed?

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Variable IEEE 754 Significand NaN Arithmetic shift Argument Modulo operation Array Bitwise operation Control structure

Back to the glossary