In mathematics, a **binary operation** or **dyadic operation** is a rule for combining two elements (called operands) to produce another element. More formally, a binary operation is an operation of arity two.

More specifically, a binary operation *on a set* is an operation whose two domains and the codomain are the same set. Examples include the familiar arithmetic operations of addition, subtraction, and multiplication. Other examples are readily found in different areas of mathematics, such as vector addition, matrix multiplication, and conjugation in groups.

An operation of arity two that involves several sets is sometimes also called a *binary operation*. For example, scalar multiplication of vector spaces takes a scalar and a vector to produce a vector, and scalar product takes two vectors to produce a scalar. Such binary operations may be called simply binary functions.

Binary operations are the keystone of most algebraic structures that are studied in algebra, in particular in semigroups, monoids, groups, rings, fields, and vector spaces.

More precisely, a binary operation on a set *S* is a mapping of the elements of the Cartesian product *S* × *S* to *S*:^{[1]}^{[2]}^{[3]}

Because the result of performing the operation on a pair of elements of *S* is again an element of *S*, the operation is called a **closed** (or **internal**) binary operation on *S* (or sometimes expressed as having the property of closure).^{[4]}

If *f* is not a function, but a partial function, then *f* is called a **partial binary operation**. For instance, division of real numbers is a partial binary operation, because one can't divide by zero: *a*/0 is undefined for every real number *a*. In both universal algebra and model theory, binary operations are required to be defined on all elements of *S* × *S*.

Sometimes, especially in computer science, the term binary operation is used for any binary function.

Typical examples of binary operations are the addition (+) and multiplication (×) of numbers and matrices as well as composition of functions on a single set. For instance,

- On the set of real numbers
**R**,*f*(*a*,*b*) =*a*+*b*is a binary operation since the sum of two real numbers is a real number. - On the set of natural numbers
**N**,*f*(*a*,*b*) =*a*+*b*is a binary operation since the sum of two natural numbers is a natural number. This is a different binary operation than the previous one since the sets are different. - On the set M(2,
**R**) of 2 × 2 matrices with real entries,*f*(*A*,*B*) =*A*+*B*is a binary operation since the sum of two such matrices is a 2 × 2 matrix. - On the set M(2,
**R**) of 2 × 2 matrices with real entries,*f*(*A*,*B*) =*AB*is a binary operation since the product of two such matrices is a 2 × 2 matrix. - For a given set
*C*, let*S*be the set of all functions*h*:*C*→*C*. Define*f*:*S*×*S*→*S*by*f*(*h*_{1},*h*_{2})(*c*) = (*h*_{1}∘*h*_{2}) (*c*) =*h*_{1}(*h*_{2}(*c*)) for all*c*∈*C*, the composition of the two functions*h*_{1}and*h*_{2}in*S*. Then*f*is a binary operation since the composition of the two functions is again a function on the set*C*(that is, a member of*S*).

Many binary operations of interest in both algebra and formal logic are commutative, satisfying *f*(*a*, *b*) = *f*(*b*, *a*) for all elements *a* and *b* in *S*, or associative, satisfying *f*(*f*(*a*, *b*), *c*) = *f*(*a*, *f*(*b*, *c*)) for all *a*, *b*, and *c* in *S*. Many also have identity elements and inverse elements.

The first three examples above are commutative and all of the above examples are associative.

On the set of real numbers **R**, subtraction, that is, *f*(*a*, *b*) = *a* − *b*, is a binary operation which is not commutative since, in general, *a* − *b* ≠ *b* − *a*. It is also not associative, since, in general, *a* − (*b* − *c*) ≠ (*a* − *b*) − *c*; for instance, 1 − (2 − 3) = 2 but (1 − 2) − 3 = −4.

On the set of natural numbers **N**, the binary operation exponentiation, *f*(*a*,*b*) = *a*^{b}, is not commutative since, *a*^{b} ≠ *b*^{a} (cf. Equation x^{y} = y^{x}), and is also not associative since *f*(*f*(*a*, *b*), *c*) ≠ *f*(*a*, *f*(*b*, *c*)). For instance, with *a* = 2, *b* = 3, and *c* = 2, *f*(2^{3},2) = *f*(8,2) = 8^{2} = 64, but *f*(2,3^{2}) = *f*(2,9) = 2^{9} = 512. By changing the set **N** to the set of integers **Z**, this binary operation becomes a partial binary operation since it is now undefined when *a* = 0 and *b* is any negative integer. For either set, this operation has a *right identity* (which is 1) since *f*(*a*, 1) = *a* for all *a* in the set, which is not an *identity* (two sided identity) since *f*(1, *b*) ≠ *b* in general.

Division (/), a partial binary operation on the set of real or rational numbers, is not commutative or associative. Tetration (↑↑), as a binary operation on the natural numbers, is not commutative or associative and has no identity element.

Binary operations are often written using infix notation such as *a* ∗ *b*, *a* + *b*, *a* · *b* or (by juxtaposition with no symbol) *ab* rather than by functional notation of the form *f*(*a*, *b*). Powers are usually also written without operator, but with the second argument as superscript.

Binary operations are sometimes written using prefix or (more frequently) postfix notation, both of which dispense with parentheses. They are also called, respectively, Polish notation and reverse Polish notation.

A binary operation, *ab*, depends on the ordered pair (*a, b*) and so (*ab*)*c* (where the parentheses here mean first operate on the ordered pair (*a*, *b*) and then operate on the result of that using the ordered pair ((*ab*), *c*)) depends in general on the ordered pair ((*a*, *b*), *c*). Thus, for the general, non-associative case, binary operations can be represented with binary trees.

However:

- If the operation is associative, (
*ab*)*c*=*a*(*bc*), then the value of (*ab*)*c*depends only on the tuple (*a*,*b*,*c*). - If the operation is commutative,
*ab*=*ba*, then the value of (*ab*)*c*depends only on { {*a*,*b*},*c*}, where braces indicate multisets. - If the operation is both associative and commutative then the value of (
*ab*)*c*depends only on the multiset {*a*,*b*,*c*}. - If the operation is associative, commutative, and idempotent,
*aa*=*a*, then the value of (*ab*)*c*depends only on the set {*a*,*b*,*c*}.

A binary operation *f* on a set *S* may be viewed as a ternary relation on *S*, that is, the set of triples (*a*, *b*, *f*(*a,b*)) in *S* × *S* × *S* for all *a* and *b* in *S*.

An **external binary operation** is a binary function from *K* × *S* to *S*. This differs from a *binary operation on a set* in the sense in that *K* need not be *S*; its elements come from *outside*.

An example of an external binary operation is scalar multiplication in linear algebra. Here *K* is a field and *S* is a vector space over that field.

Some external binary operations may alternatively be viewed as an action of *K* on *S*. This requires the existence of an associative multiplication in *K*, and a compatibility rule of the form where and (here, both the external operation and the multiplication in *K* are denoted by juxtaposition).

The dot product of two vectors maps *S* × *S* to *K*, where *K* is a field and *S* is a vector space over *K*. It depends on authors whether it is considered as a binary operation.