Division by two
   HOME

TheInfoList



OR:

In
mathematics Mathematics is an area of knowledge that includes the topics of numbers, formulas and related structures, shapes and the spaces in which they are contained, and quantities and their changes. These topics are represented in modern mathematics ...
, division by
two 2 (two) is a number, numeral and digit. It is the natural number following 1 and preceding 3. It is the smallest and only even prime number. Because it forms the basis of a duality, it has religious and spiritual significance in many cultur ...
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 A multiplication algorithm is an algorithm (or method) to multiply two numbers. Depending on the size of the numbers, different algorithms are more efficient than others. Efficient multiplication algorithms have existed since the advent of the d ...
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 is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
.. 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 of the Hindu–Arabic numeral ...
, in the binary numeral system used in computer programming, and in other even-numbered bases.


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 oper ...
operation that shifts the number one place to the right. This is a form of
strength reduction In compiler construction, strength reduction is a compiler optimization where expensive operations are replaced with equivalent but less expensive operations. The classic example of strength reduction converts "strong" multiplications inside a loo ...
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 two as the base and integer  as the exponent. In a context where only integers are considered, is restricted to non-negative ...
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 o ...
. However, for the sake of
software portability A computer program is said to be portable if there is very low effort required to make it run on different platforms. The pre-requirement for portability is the generalized abstraction between the application logic and system interfaces. When ...
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 translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs tha ...
to perform this replacement. An example from Common Lisp: (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 (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
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, 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 underflow gap around zero in floating-point arithmetic. Any non-zero number with magnitude smaller than the smallest normal ...
). 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, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
provides the method java.lang.Math.scalb for scaling by a power of two, and the C programming language 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 rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
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 Even may refer to: General * Even (given name), a Norwegian male personal name * Even (surname) * Even (people), an ethnic group from Siberia and Russian Far East ** Even language, a language spoken by the Evens * Odd and Even, a solitaire game w ...
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 In mathematics, zero is an even number. In other words, its parity—the quality of an integer being even or odd—is even. This can be easily verified based on the definition of "even": it is an integer multiple of 2, specifically . As a r ...
. If the last digit of ''N'' is
odd Odd means unpaired, occasional, strange or unusual, or a person who is viewed as eccentric. Odd may also refer to: Acronym * ODD (Text Encoding Initiative) ("One Document Does it all"), an abstracted literate-programming format for describing X ...
digit one should add 0.5 to the result.


See also

* One half * Median, a value that splits a set of data values into two equal subsets * Bisection, the partition of a geometric object into two equal halves *
Dimidiation In heraldry, dimidiation is a method of marshalling (heraldically combining) two coats of arms. For a time, dimidiation preceded the method known as impalement. Whereas impalement involves placing the whole of both coats of arms side by side ...
, 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)