# Division by two

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Division_by_two
> Markdown URL: https://mediated.wiki/source/Division_by_two.md
> Source: https://en.wikipedia.org/wiki/Division_by_two
> Source revision: 1339609103
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|None}}
thumb | right | alt=An orange on a white plate that has been divided in half.  | An orange that has been sliced into two halves.

In [mathematics](/source/mathematics), '''division by two''', also called '''halving''', '''mediation''', or '''dimidiation''', is common in [formulas](/source/mathematical_formula) and as a step in [arithmetic](/source/arithmetic)al calculations; it is equivalent to [multiplication](/source/multiplication) by [one half](/source/one_half).<ref>{{citation |title=The Earliest arithmetics in English |volume=118 |series=Early English Text Society |first=Robert |last=Steele |publisher=Oxford University Press |year=1922 |page=82 }}.</ref> Starting with an arbitrary number or quantity {{tmath|x}}, its [division](/source/Division_(mathematics)) by [two](/source/two) can be written as any of the following equivalent expressions: <math display=block>x \div 2,\ x / 2,\ \frac x 2,\ \tfrac12 x,\ 0.5 x.</math>

The treatment of this as a different operation from multiplication and division by other numbers goes back to the ancient Egyptians, whose [multiplication algorithm](/source/Ancient_Egyptian_multiplication) used division by two as one of its fundamental steps.<ref>{{citation |title=A history of algorithms: from the pebble to the microchip |first1=Jean-Luc |last1=Chabert |first2=Évelyne |last2=Barbin|publisher=Springer-Verlag |year=1999 |isbn=978-3-540-63369-3 |page=16 }}.</ref>
Some mathematicians as late as the sixteenth century continued to view halving as a separate operation,<ref>{{citation|title=The educational significance of sixteenth century arithmetic from the point of view of the present time |volume=8 |series=Contributions to education |first=Lambert Lincoln |last=Jackson |publisher=Columbia University |year=1906 |page=76 }}.</ref><ref>{{citation |title=A Fifteenth Century French Algorism from Liége |journal=Isis |volume=12 |issue=2 |year=1929 |first=E. G. R. |last=Waters |pages=194–236 |jstor=224785 |doi=10.1086/346408 |s2cid=144157808}}.</ref> and it often continues to be treated separately in modern [computer programming](/source/computer_programming).<ref name="WC00">{{citation |title=Software optimization for high-performance computing |first1=Kevin R.|last1=Wadleigh|first2=Isom L.|last2=Crawford|publisher=Prentice Hall |year=2000 |page=[https://archive.org/details/softwareoptimiza0000wadl/page/92 92] |isbn=978-0-13-017008-8 |url=https://archive.org/details/softwareoptimiza0000wadl/page/92 }}.</ref>
Performing this operation is simple in [decimal arithmetic](/source/decimal_arithmetic), in the [binary numeral system](/source/binary_numeral_system) used in computer programming, and in other even-numbered [base](/source/numeral_system)s.

==Binary==
In binary arithmetic, division by two can be performed by a [bit shift](/source/bit_shift) operation that shifts the number one place to the right.
This is a form of [strength reduction](/source/strength_reduction) optimization. For example, 1101001 in binary (the decimal number 105), shifted one place to the right, is 110100 (the decimal number 52): the lowest order bit, a 1, is removed. Similarly, division by any [power of two](/source/power_of_two) 2<sup>''k''</sup> may be performed by right-shifting ''k'' positions. Because bit shifts are often much faster operations than division, replacing a division by a shift in this way can be a helpful step in [program optimization](/source/program_optimization).<ref name="WC00"/> However, for the sake of [software portability](/source/software_portability) and readability, it is often best to write programs using the division operation and trust in the [compiler](/source/compiler) to perform this replacement.<ref>{{citation|title=Write portable code: an introduction to developing software for multiple platforms|first=Brian|last=Hook|publisher=No Starch Press|year=2005|isbn=978-1-59327-056-8|page=133}}.</ref> An example from [Common Lisp](/source/Common_Lisp):

<syntaxhighlight lang="lisp">
 (setq number #b1101001)   ; #b1101001  —  105
 (ash number -1)           ; #b0110100  —  105 >> 1 ⇒ 52
 (ash number -4)           ; #b0000110  —  105 >> 4 ≡ 105 / 2⁴ ⇒ 6
</syntaxhighlight>

The above statements, however, are not always true when dealing with dividing [signed](/source/Signed_number_representations) binary numbers. Shifting right by 1 bit will divide by two, always rounding down. However, in some languages, division of signed binary numbers round towards 0 (which, if the result is negative, means it rounds up). For example, [Java](/source/Java_(programming_language)) is one such language: in Java, <code>-3 / 2</code> evaluates to <code>-1</code>, whereas <code>-3 >> 1</code> evaluates to <code>-2</code>. So in this case, the compiler ''cannot'' optimize division by two by replacing it by a bit shift, when the dividend could possibly be negative.

==Binary floating point==
In binary [floating-point arithmetic](/source/floating-point_arithmetic), division by two can be performed by decreasing the exponent by one (as long as the result is not a [subnormal number](/source/subnormal_number)). Many programming languages provide functions that can be used to divide a floating point number by a power of two. For example, the [Java programming language](/source/Java_(programming_language)) provides the method <code>java.lang.Math.scalb</code> for scaling by a power of two,<ref>{{cite web
|url=http://java.sun.com/javase/6/docs/api/java/lang/Math.html#scalb(double,%20int)
|title=Math.scalb
|work=Java Platform Standard Ed. 6
|accessdate=2009-10-11
}}</ref> and the [C programming language](/source/C_(programming_language)) provides the function <code>ldexp</code> for the same purpose.<ref>{{citation
|title=Programming languages — C, International Standard ISO/IEC 9899:1999
}}, Section 7.12.6.6.</ref>

==Decimal==
The following [algorithm](/source/algorithm) is for decimal. However, it can be used as a model to construct an algorithm for taking half of any number ''N'' in any [even](/source/even_and_odd_numbers) base.
*Write out ''N'', putting a zero to its left.
*Go through the digits of ''N'' in overlapping pairs, writing down digits of the result from the following table.

{| class="wikitable"
|-
! If first digit is
| Even || Even || Even || Even || Even
| Odd || Odd || Odd || Odd || Odd
|-
! And second digit is
| 0 or 1 || 2 or 3 || 4 or 5 || 6 or 7 || 8 or 9
| 0 or 1 || 2 or 3 || 4 or 5 || 6 or 7 || 8 or 9
|-
! Write
| 0 || 1 || 2 || 3 || 4
| 5 || 6 || 7 || 8 || 9
|}

Example: 1738/2=?

Write 01738. We will now work on finding the result.
* 01: even digit followed by 1, write 0.
* 17: odd digit followed by 7, write 8.
* 73: odd digit followed by 3, write 6.
* 38: odd digit followed by 8, write 9.
Result: 0869.

From the example one can see that [0 is even](/source/0_is_even).

If the last digit of ''N'' is [odd](/source/even_and_odd_numbers) digit one should add 0.5 to the result.

==See also==
*[One half](/source/One_half)
*[Median](/source/Median), a value that splits a set of data values into two equal subsets
*[Bisection](/source/Bisection), the partition of a geometric object into two equal halves
*[Dimidiation](/source/Dimidiation), a heraldic method of joining two coats of arms by splitting their designs into halves

==References==
{{reflist}}

Two
Category:Elementary arithmetic
Category:Binary arithmetic
Category:Parity (mathematics)
Category:2 (number)

---
Adapted from the Wikipedia article [Division by two](https://en.wikipedia.org/wiki/Division_by_two) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Division_by_two?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
