In
mathematics
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
, division by
two or halving has also been called mediation or dimidiation. The treatment of this as a different operation from multiplication and division by other numbers goes back to the ancient Egyptians, whose
multiplication algorithm used division by two as one of its fundamental steps.
Some mathematicians as late as the sixteenth century continued to view halving as a separate operation, and it often continues to be treated separately in modern
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
.
[.]
Performing this operation is simple in
decimal arithmetic
The decimal numeral system (also called the base-ten positional numeral system and denary or decanary) is the standard system for denoting integer and non-integer numbers. It is the extension to non-integer numbers (''decimal fractions'') of the ...
, in the
binary numeral system
A binary number is a number expressed in the base-2 numeral system or binary numeral system, a method for representing numbers that uses only two symbols for the natural numbers: typically "0" ( zero) and "1" ( one). A ''binary number'' may als ...
used in computer programming, and in other even-numbered
bases. To
divide an
odd number
In mathematics, parity is the property of an integer of whether it is even or odd. An integer is even if it is divisible by 2, and odd if it is not.. For example, −4, 0, and 82 are even numbers, while −3, 5, 23, and 69 are odd numbers.
The ...
by
2 use the
mathematical solution ((N−1)÷2)+0.5. For example, if N=7, then ((7−1)÷2)+0.5=3.5, so 7÷2=3.5.
Binary
In binary arithmetic, division by two can be performed by a
bit shift
In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic opera ...
operation that shifts the number one place to the right.
This is a form of
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
A power of two is a number of the form where is an integer, that is, the result of exponentiation with number 2, two as the Base (exponentiation), base and integer as the exponent. In the fast-growing hierarchy, is exactly equal to f_1^ ...
2
''k'' 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
In computer science, program optimization, code optimization, or software optimization is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources. In general, a computer program may be op ...
.
However, for the sake of
software portability
Software consists of computer programs that instruct the execution of a computer. Software also includes design documents and specifications.
The history of software is closely tied to the development of digital computers in the mid-20th ...
and readability, it is often best to write programs using the division operation and trust in the
compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
to perform this replacement. An example from
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
:
(setq number #b1101001) ; #b1101001 — 105
(ash number -1) ; #b0110100 — 105 >> 1 ⇒ 52
(ash number -4) ; #b0000110 — 105 >> 4 ≡ 105 / 2⁴ ⇒ 6
The above statements, however, are not always true when dealing with dividing
signed 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
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
is one such language: in Java,
-3 / 2
evaluates to
-1
, whereas
-3 >> 1
evaluates to
-2
. 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
In computing, floating-point arithmetic (FP) is arithmetic on subsets of real numbers formed by a ''significand'' (a Sign (mathematics), signed sequence of a fixed number of digits in some Radix, base) multiplied by an integer power of that ba ...
, division by two can be performed by decreasing the exponent by one (as long as the result is not a
subnormal number
In computer science, subnormal numbers are the subset of denormalized numbers (sometimes called denormals) that fill the arithmetic underflow, underflow gap around zero in floating-point arithmetic. Any non-zero number with magnitude smaller than ...
). 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
Java is a high-level, general-purpose, memory-safe, object-oriented programming language. It is intended to let programmers ''write once, run anywhere'' ( WORA), meaning that compiled Java code can run on all platforms that support Jav ...
provides the method
java.lang.Math.scalb
for scaling by a power of two, and the
C programming language
C (''pronounced'' '' – like the letter c'') is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of ...
provides the function
ldexp
for the same purpose.
[, Section 7.12.6.6.]
Decimal
The following
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
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 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.
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.
If the last digit of ''N'' is
odd digit one should add 0.5 to the result.
See also
*
One half
One half is the multiplicative inverse of 2. It is an irreducible fraction with a numerator of 1 and a denominator of 2. It often appears in mathematical equations, recipes and measurements.
As a word
One half is one of the few fractions ...
*
Median
The median of a set of numbers is the value separating the higher half from the lower half of a Sample (statistics), data sample, a statistical population, population, or a probability distribution. For a data set, it may be thought of as the “ ...
, a value that splits a set of data values into two equal subsets
*
Bisection
In geometry, bisection is the division of something into two equal or congruent parts (having the same shape and size). Usually it involves a bisecting line, also called a ''bisector''. The most often considered types of bisectors are the ''s ...
, the partition of a geometric object into two equal halves
*
Dimidiation
In heraldry, dimidiation is a method of Heraldry#Marshalling, marshalling (heraldically combining) two coat of arms, coats of arms.
For a time, dimidiation preceded the method known as Impalement (heraldry), impalement. Whereas impalement inv ...
, a heraldic method of joining two coats of arms by splitting their designs into halves
References
{{reflist
Division (mathematics)
Elementary arithmetic
Binary arithmetic
Parity (mathematics)
2 (number)