Difference between revisions of "cpp/language/operator arithmetic"
(Added version marks for some fundamental types.) |
(Added CWG issue #2853 DR.) |
||
(31 intermediate revisions by 8 users not shown) | |||
Line 3: | Line 3: | ||
Returns the result of specific arithmetic operation. | Returns the result of specific arithmetic operation. | ||
− | {|class="wikitable" style="font-size:85%;" | + | {|class="wikitable"<!--style="font-size:85%;"--> |
|- | |- | ||
− | ! rowspan="2"|Operator name | + | !rowspan="2"|Operator name |
− | ! rowspan="2"|Syntax | + | !rowspan="2"|Syntax |
− | + | !colspan="2"|Prototype examples (for {{c/core|class T}}) | |
− | ! colspan="2"|Prototype examples (for {{c|class T}}) | + | |
|- | |- | ||
− | ! Inside class definition | + | !Inside class definition |
− | ! Outside class definition | + | !Outside class definition |
|- | |- | ||
− | | | + | |Unary plus |
− | |{{ | + | |{{co|+a}} |
− | + | ||
|{{c|T T::operator+() const;}} | |{{c|T T::operator+() const;}} | ||
− | |{{c|T operator+(const T &a);}} | + | |{{c|T operator+(const T& a);}} |
|- | |- | ||
− | | | + | |Unary minus |
− | |{{ | + | |{{co|-a}} |
− | + | ||
|{{c|T T::operator-() const;}} | |{{c|T T::operator-() const;}} | ||
− | |{{c|T operator-(const T &a);}} | + | |{{c|T operator-(const T& a);}} |
|- | |- | ||
− | | | + | |Addition |
− | |{{ | + | |{{co|a + b}} |
− | + | |{{c|T T::operator+(const T2& b) const;}} | |
− | |{{c|T T::operator+(const T2 &b) const;}} | + | |{{c|T operator+(const T& a, const T2& b);}} |
− | |{{c|T operator+(const T &a, const T2 &b);}} | + | |
|- | |- | ||
− | | | + | |Subtraction |
− | |{{ | + | |{{co|a - b}} |
− | + | |{{c|T T::operator-(const T2& b) const;}} | |
− | |{{c|T T::operator-(const T2 &b) const;}} | + | |{{c|T operator-(const T& a, const T2& b);}} |
− | |{{c|T operator-(const T &a, const T2 &b);}} | + | |
|- | |- | ||
− | | | + | |Multiplication |
− | |{{ | + | |{{co|a * b}} |
− | + | |{{c|T T::operator*(const T2& b) const;}} | |
− | |{{c|T T::operator*(const T2 &b) const;}} | + | |{{c|T operator*(const T& a, const T2& b);}} |
− | |{{c|T operator*(const T &a, const T2 &b);}} | + | |
|- | |- | ||
− | | | + | |Division |
− | |{{ | + | |{{co|a / b}} |
− | + | |{{c|T T::operator/(const T2& b) const;}} | |
− | |{{c|T T::operator/(const T2 &b) const;}} | + | |{{c|T operator/(const T& a, const T2& b);}} |
− | |{{c|T operator/(const T &a, const T2 &b);}} | + | |
|- | |- | ||
− | | | + | |Remainder |
− | |{{ | + | |{{co|a % b}} |
− | + | |{{c|T T::operator%(const T2& b) const;}} | |
− | |{{c|T T::operator%(const T2 &b) const;}} | + | |{{c|T operator%(const T& a, const T2& b);}} |
− | |{{c|T operator%(const T &a, const T2 &b);}} | + | |
|- | |- | ||
− | | | + | |Bitwise NOT |
− | |{{ | + | |{{co|~a}} |
− | + | ||
|{{c|T T::operator~() const;}} | |{{c|T T::operator~() const;}} | ||
− | |{{c|T operator~(const T &a);}} | + | |{{c|T operator~(const T& a);}} |
|- | |- | ||
− | | | + | |Bitwise AND |
− | |{{ | + | |{{co|a & b}} |
− | + | |{{c|T T::operator&(const T2& b) const;}} | |
− | |{{c|T T::operator&(const T2 &b) const;}} | + | |{{c|T operator&(const T& a, const T2& b);}} |
− | |{{c|T operator&(const T &a, const T2 &b);}} | + | |
|- | |- | ||
− | | | + | |Bitwise OR |
− | |{{ | + | |{{co|a {{!}} b}} |
− | + | |{{c|T T::operator{{!}}(const T2& b) const;}} | |
− | |{{c|T T::operator{{!}}(const T2 &b) const;}} | + | |{{c|T operator{{!}}(const T& a, const T2& b);}} |
− | |{{c|T operator{{!}}(const T &a, const T2 &b);}} | + | |
|- | |- | ||
− | | | + | |Bitwise XOR |
− | |{{ | + | |{{co|a ^ b}} |
− | + | |{{c|T T::operator^(const T2& b) const;}} | |
− | |{{c|T T::operator^(const T2 &b) const;}} | + | |{{c|T operator^(const T& a, const T2& b);}} |
− | |{{c|T operator^(const T &a, const T2 &b);}} | + | |
|- | |- | ||
− | | | + | |Bitwise left shift |
− | |{{ | + | |{{co|a << b}} |
− | + | |{{c|T T::operator<<(const T2& b) const;}} | |
− | |{{c|T T::operator<<(const T2 &b) const;}} | + | |{{c|T operator<<(const T& a, const T2& b);}} |
− | |{{c|T operator<<(const T &a, const T2 &b);}} | + | |
|- | |- | ||
− | | | + | |Bitwise right shift |
− | |{{ | + | |{{co|a >> b}} |
− | + | |{{c|T T::operator>>(const T2& b) const;}} | |
− | |{{c|T T::operator>>(const T2 &b) const;}} | + | |{{c|T operator>>(const T& a, const T2& b);}} |
− | |{{c|T operator>>(const T &a, const T2 &b);}} | + | |
|- | |- | ||
|colspan="5"| | |colspan="5"| | ||
:'''Notes'''<br> | :'''Notes'''<br> | ||
− | * All built-in operators return values, and most {{rlp|operators|user-defined overloads}} also return values so that the user-defined operators can be used in the same manner as the built-ins. However, in a user-defined operator overload, any type can be used as return type (including {{c|void}}). In particular, stream insertion and stream extraction overloads of {{ | + | * All operators in this table are {{rlp|operators|overloadable}}. |
− | * {{tt|T2}} can be any type including {{tt|T}} | + | * All built-in operators return values, and most {{rlp|operators|user-defined overloads}} also return values so that the user-defined operators can be used in the same manner as the built-ins. However, in a user-defined operator overload, any type can be used as return type (including {{c/core|void}}). In particular, stream insertion and stream extraction overloads of {{c/core|operator<<}} and {{c/core|operator>>}} return {{tt|T&}}. |
+ | * {{tt|T2}} can be any type including {{tt|T}}. | ||
|} | |} | ||
− | === | + | ===General explanation=== |
− | + | All built-in arithmetic operators compute the result of specific arithmetic operation and returns its result. The arguments are not modified. | |
− | All arithmetic operators compute the result of specific arithmetic operation and returns its result. The arguments are not modified. | + | |
====Conversions==== | ====Conversions==== | ||
− | If the operand passed to | + | If the operand passed to a built-in arithmetic operator is integral or unscoped enumeration type, then before any other action (but after lvalue-to-rvalue conversion, if applicable), the operand undergoes {{rlpsd|implicit conversion#Integral promotion}}. If an operand has array or function type, {{rlp|implicit conversion#Array-to-pointer conversion|array-to-pointer}} and {{rlpsd|implicit conversion#Function-to-pointer}} conversions are applied. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | For the binary operators (except shifts), if the promoted operands have different types, {{rlp|usual arithmetic conversions}} are applied. | |
====Overflows==== | ====Overflows==== | ||
− | Unsigned integer arithmetic is always performed {{math|modulo 2{{su|p=n}}}} where n is the number of bits in that particular integer. E.g. for {{c|unsigned int}}, adding one to {{lc|UINT_MAX}} gives {{c|0}}, and subtracting one from {{c|0}} gives {{lc|UINT_MAX}}. | + | Unsigned integer arithmetic is always performed {{math|modulo 2{{su|p=n}}}} where n is the number of bits in that particular integer. E.g. for {{c/core|unsigned int}}, adding one to {{lc|UINT_MAX}} gives {{c|0}}, and subtracting one from {{c|0}} gives {{lc|UINT_MAX}}. |
When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined, — the possible manifestations of such an operation include: | When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined, — the possible manifestations of such an operation include: | ||
− | * it wraps around according to the rules of the representation (typically | + | * it wraps around according to the rules of the representation (typically {{rlp|types#Range of values|two's complement}}), |
* it traps — on some platforms or due to compiler options (e.g. {{tt|-ftrapv}} in GCC and Clang), | * it traps — on some platforms or due to compiler options (e.g. {{tt|-ftrapv}} in GCC and Clang), | ||
* it saturates to minimal or maximal value (on many DSPs), | * it saturates to minimal or maximal value (on many DSPs), | ||
− | * it is completely [ | + | * it is completely [https://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html optimized out by the compiler]. |
====Floating-point environment==== | ====Floating-point environment==== | ||
− | If {{ltt|cpp/preprocessor/impl|#pragma STDC FENV_ACCESS}} is supported and set to {{tt|ON}}, all floating-point arithmetic operators obey the current floating-point [[cpp/numeric/fenv/FE_round|rounding direction]] and report floating-point arithmetic errors as specified in {{ltt|cpp/numeric/math/math_errhandling}} unless part of a {{rlp|initialization#Non-local variables|static initializer}} (in which case floating-point exceptions are not raised and the rounding mode is to nearest) | + | If {{ltt|cpp/preprocessor/impl|#pragma STDC FENV_ACCESS}} is supported and set to {{tt|ON}}, all floating-point arithmetic operators obey the current floating-point [[cpp/numeric/fenv/FE_round|rounding direction]] and report floating-point arithmetic errors as specified in {{ltt|cpp/numeric/math/math_errhandling}} unless part of a {{rlp|initialization#Non-local variables|static initializer}} (in which case floating-point exceptions are not raised and the rounding mode is to nearest). |
====Floating-point contraction==== | ====Floating-point contraction==== | ||
− | Unless {{ltt|cpp/preprocessor/impl|#pragma STDC FP_CONTRACT}} is supported and set to {{tt|OFF}}, all floating-point arithmetic may be performed as if the intermediate results have infinite range and precision, that is, optimizations that omit rounding errors and floating-point exceptions are allowed. For example, C++ allows the implementation of {{c|(x * y) + z}} with a single fused multiply-add CPU instruction or optimization of {{c|1=a = x * x * x * x;}} as {{c|1=tmp = x * x; a = tmp * tmp}}. {{mark unreviewed dr|LWG}}<!-- confirmed in 2016 by LWG 238 --> | + | Unless {{ltt|cpp/preprocessor/impl|#pragma STDC FP_CONTRACT}} is supported and set to {{tt|OFF}}, all floating-point arithmetic may be performed as if the intermediate results have infinite range and precision, that is, optimizations that omit rounding errors and floating-point exceptions are allowed. For example, C++ allows the implementation of {{c|(x * y) + z}} with a single fused multiply-add CPU instruction or optimization of {{c|1=a = x * x * x * x;}} as {{c|1=tmp = x * x; a = tmp * tmp}}. {{mark unreviewed dr|LWG}}<!--confirmed in 2016 by LWG 238--> |
− | Unrelated to contracting, intermediate results of floating-point arithmetic may have range and precision that is different from the one indicated by its type, see {{lc|FLT_EVAL_METHOD}} | + | Unrelated to contracting, intermediate results of floating-point arithmetic may have range and precision that is different from the one indicated by its type, see {{lc|FLT_EVAL_METHOD}}. |
Formally, the C++ standard makes no guarantee on the accuracy of floating-point operations. | Formally, the C++ standard makes no guarantee on the accuracy of floating-point operations. | ||
− | + | ===Unary arithmetic operators=== | |
− | The unary arithmetic operator expressions have the form | + | The unary arithmetic operator expressions have the form |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 152: | Line 118: | ||
{{sdsc|num=2|{{ttb|-}} {{spar|expression}}}} | {{sdsc|num=2|{{ttb|-}} {{spar|expression}}}} | ||
{{sdsc end}} | {{sdsc end}} | ||
− | @1@ | + | @1@ Unary plus (promotion). |
− | + | @2@ Unary minus (negation). | |
− | @2@ | + | |
− | + | ||
− | + | Unary {{tt|+}} and {{tt|-}} operators have higher {{rlp|operator precedence|precedence}} than all binary arithmetic operators, so {{spar|expression}} cannot contain top-level binary arithmetic operators. These operators associate from right to left: | |
+ | {{source| | ||
+ | +a - b; // equivalent to (+a) - b, NOT +(a - b) | ||
+ | -c + d; // equivalent to (-c) + d, NOT -(c + d) | ||
− | + | +-e; // equivalent to +(-e), the unary + is a no-op if “e” is a built-in type | |
+ | // because any possible promotion is performed during negation already | ||
+ | }} | ||
+ | ====Built-in unary arithmetic operators==== | ||
+ | @1@ For the built-in unary plus operator, {{spar|expression}} must be a prvalue of arithmetic, unscoped enumeration, or pointer type. Integral promotion is performed on {{spar|expression}} if it has integral or unscoped enumeration type. The type of the result is the (possibly promoted) type of {{spar|expression}}. | ||
+ | @@ The result of the built-in promotion is the value of {{spar|expression}}. The built-in unary operation is no-op if the operand is a prvalue of a promoted integral type or a pointer type. Otherwise, the type or value category of the operand is changed by integral promotion or lvalue-to-rvalue, array-to-pointer, function-to-pointer, or user-defined conversion. For example, {{c/core|char}} is converted to {{c/core|int}} {{rev inl|since=c++11|, and non-generic captureless {{rlp|lambda|lambda expression}} is converted to function pointer}} in unary plus expressions. | ||
+ | |||
+ | @2@ For the built-in unary minus operator, {{spar|expression}} must be a prvalue of arithmetic or unscoped enumeration type. Integral promotion is performed on {{spar|expression}}. The type of the result is the type of the promoted type of {{spar|expression}}. | ||
+ | @@ The result of the built-in negation is the negative of the promoted {{spar|expression}}. For unsigned {{c|a}}, the value of {{c|-a}} is {{mathjax-or|\({\small 2^N-a}\)|2{{su|p=N}}-a}}, where {{c|N}} is the number of bits after promotion. | ||
+ | * In other words, the result is the two’s complement of the operand (where operand and result are considered as unsigned). | ||
+ | |||
+ | {{anchor|Overloads of unary arithmetic operators}} | ||
+ | |||
+ | ====Overloads==== | ||
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every cv-unqualified promoted arithmetic type {{tt|A}} and for every type {{tt|T}}, the following function signatures participate in overload resolution: | In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every cv-unqualified promoted arithmetic type {{tt|A}} and for every type {{tt|T}}, the following function signatures participate in overload resolution: | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl|A operator+(A)}} | {{dcl|A operator+(A)}} | ||
{{dcl|T* operator+(T*)}} | {{dcl|T* operator+(T*)}} | ||
− | {{dcl|A operator-(A)}} | + | {{dcl|A operator-(A)}} |
{{dcl end}} | {{dcl end}} | ||
{{example | {{example | ||
− | |||
|code= | |code= | ||
#include <iostream> | #include <iostream> | ||
Line 179: | Line 158: | ||
unsigned char n2 = 1; | unsigned char n2 = 1; | ||
unsigned int n3 = 1; | unsigned int n3 = 1; | ||
− | std::cout << "char: " << c << " int: " << +c << | + | std::cout << "char: " << c << " int: " << +c << "\n" |
− | + | "-1, where 1 is signed: " << -n1 << "\n" | |
− | + | "-1, where 1 is unsigned char: " << -n2 << "\n" | |
− | + | "-1, where 1 is unsigned int: " << -n3 << '\n'; | |
char a[3]; | char a[3]; | ||
− | std::cout << "size of array: " << sizeof a << | + | std::cout << "size of array: " << sizeof a << "\n" |
− | + | "size of pointer: " << sizeof +a << '\n'; | |
} | } | ||
+ | |p=true | ||
|output= | |output= | ||
char: j int: 106 | char: j int: 106 | ||
Line 196: | Line 176: | ||
}} | }} | ||
− | + | ===Additive operators=== | |
− | The | + | The additive operator expressions have the form |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 203: | Line 183: | ||
{{sdsc|num=2|{{spar|lhs}} {{ttb|-}} {{spar|rhs}}}} | {{sdsc|num=2|{{spar|lhs}} {{ttb|-}} {{spar|rhs}}}} | ||
{{sdsc end}} | {{sdsc end}} | ||
− | @1@ addition | + | @1@ Binary plus (addition). |
− | + | @2@ Binary minus (subtraction). | |
− | + | ||
− | + | ||
− | @2@ subtraction | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | Binary {{tt|+}} and {{tt|-}} operators have higher {{rlp|operator precedence|precedence}} than all other binary arithmetic operators except {{tt|*}}, {{tt|/}} and {{tt|%}}. These operators associate from left to right: | |
− | + | {{source| | |
− | + | a + b * c; // equivalent to a + (b * c), NOT (a + b) * c | |
− | + | d / e - f; // equivalent to (d / e) - f, NOT d / (e - f) | |
+ | g + h >> i; // equivalent to (g + h) >> i, NOT g + (h >> i) | ||
+ | |||
+ | j - k + l - m; // equivalent to ((j - k) + l) - m | ||
+ | }} | ||
+ | |||
+ | ====Built-in additive operators==== | ||
+ | For built-in binary plus and binary minus operators, both of {{spar|lhs}} and {{spar|rhs}} must be prvalues, and one of the following conditions must be satisfied: | ||
+ | * Both operands have arithmetic or unscoped enumeration type. In this case, {{rlp|usual arithmetic conversions}} are performed on both operands. | ||
+ | * Exactly one operand has integral or unscoped enumeration type. In this case, integral promotion is applied to that operand. | ||
+ | |||
+ | In the remaining description in this section, "operand(s)", {{spar|lhs}} and {{spar|rhs}} refer to the converted or promoted operand(s). | ||
+ | |||
+ | @1@ For built-in addition, one of the following conditions must be satisfied: | ||
+ | * Both operands have arithmetic type. In this case, the result is the sum of the operands. | ||
+ | * One operand is a pointer to a completely-defined object type, and the other operand has integral type. In this case, the integral value is added to the pointer (see {{lsd|#Pointer arithmetic}}). | ||
+ | @2@ For built-in subtraction, one of the following conditions must be satisfied: | ||
+ | * Both operands have arithmetic type. In this case, the result is the difference resulting from the subtraction of {{spar|rhs}} from {{spar|lhs}}. | ||
+ | * {{spar|lhs}} is a pointer to a completely-defined object type, and {{spar|rhs}} has integral type. In this case, the integral value is subtracted from the pointer (see {{lsd|#Pointer arithmetic}}). | ||
+ | * Both operands are pointers to cv-qualified or cv-unqualified versions of the same completely-defined object type. In this case {{spar|rhs}} is subtracted from {{spar|lhs}} (see {{lsd|#Pointer arithmetic}}). | ||
+ | |||
+ | If both operands have a floating-point type, and the type supports IEEE floating-point arithmetic (see {{lc|std::numeric_limits::is_iec559}}): | ||
+ | * If one operand is NaN, the result is NaN. | ||
+ | * Infinity minus infinity is NaN, and {{lc|FE_INVALID}} is raised. | ||
+ | * Infinity plus the negative infinity is NaN, and {{lc|FE_INVALID}} is raised. | ||
+ | |||
+ | ====Pointer arithmetic==== | ||
+ | When an expression {{c|J}} that has integral type is added to or subtracted from an expression {{c|P}} of pointer type, the result has the type of {{c|P}}. | ||
+ | * If {{c|P}} evaluates to a {{rlp|pointer#Null pointers|null pointer value}} and {{c|J}} evaluates to {{c|0}}, the result is a null pointer value. | ||
+ | * Otherwise, if {{c|P}} points to the {{tt|i}}th element of an array object {{c|x}} with {{c|n}} elements, given the value of {{c|J}} as {{c|j}}, {{c|P}} is added or subtracted as follows: | ||
+ | :* The expressions {{c|P + J}} and {{c|J + P}} | ||
+ | ::* point to the {{tt|i+j}}th element of {{c|x}} if {{c|i + j}} is in {{range|0|n}}, and | ||
+ | ::* are pointers past the end of the last element of {{c|x}} if {{c|i + j}} is {{c|n}}. | ||
+ | :* The expression {{c|P - J}} | ||
+ | ::* points to the {{tt|i-j}}th element of {{c|x}} if {{c|i - j}} is in {{range|0|n}}, and | ||
+ | ::* is a pointer past the end of the last element of {{c|x}} if {{c|i - j}} is {{c|n}}. | ||
+ | :* Other {{c|j}} values result in undefined behavior. | ||
+ | * Otherwise, if {{c|P}} points to a complete object, a base class subobject or a member subobject {{c|y}}, given the value of {{c|J}} as {{c|j}}, {{c|P}} is added or subtracted as follows: | ||
+ | :* The expressions {{c|P + J}} and {{c|J + P}} | ||
+ | ::* point to {{c|y}} if {{c|j}} is {{c|0}}, and | ||
+ | ::* are pointers past the end of {{c|y}} if {{c|j}} is {{c|1}}. | ||
+ | :* The expression {{c|P - J}} | ||
+ | ::* points to {{c|y}} if {{c|j}} is {{c|0}}, and | ||
+ | ::* is a pointer past the end of {{c|y}} if {{c|j}} is {{c|-1}}. | ||
+ | :* Other {{c|j}} values result in undefined behavior. | ||
+ | * Otherwise, if {{c|P}} is a pointer past the end of an object {{c|z}}, given the value of {{c|J}} as {{c|j}}: | ||
+ | :* If {{c|z}} is an array object with {{c|n}} elements, {{c|P}} is added or subtracted as follows: | ||
+ | ::* The expressions {{c|P + J}} and {{c|J + P}} | ||
+ | :::* point to the {{tt|n+j}}th element of {{c|z}} if {{c|n + j}} is in {{range|0|n}}, and | ||
+ | :::* are pointers past the end of the last element of {{c|z}} if {{c|j}} is {{c|0}}. | ||
+ | ::* The expression {{c|P - J}} | ||
+ | :::* points to the {{tt|n-j}}th element of {{c|z}} if {{c|n - j}} is in {{range|0|n}}, and | ||
+ | :::* is a pointer past the end of the last element of {{c|z}} if {{c|j}} is {{c|0}}. | ||
+ | ::* Other {{c|j}} values result in undefined behavior. | ||
+ | :* Otherwise, {{c|P}} is added or subtracted as follows: | ||
+ | ::* The expressions {{c|P + J}} and {{c|J + P}} | ||
+ | :::* point to {{c|z}} if {{c|j}} is {{c|-1}}, and | ||
+ | :::* are pointers past the end of {{c|z}} if {{c|j}} is {{c|0}}. | ||
+ | ::* The expression {{c|P - J}} | ||
+ | :::* points to {{c|z}} if {{c|j}} is {{c|1}}, and | ||
+ | :::* is a pointer past the end of {{c|z}} if {{c|j}} is {{c|0}}. | ||
+ | ::* Other {{c|j}} values result in undefined behavior. | ||
+ | * Otherwise, the behavior is undefined. | ||
+ | |||
+ | When two pointer expressions {{c|P}} and {{c|Q}} are subtracted, the type of the result is {{lc|std::ptrdiff_t}}. | ||
+ | * If {{c|P}} and {{c|Q}} both evaluate to {{rlp|pointer#Null pointers|null pointer values}}, the result is {{c|0}}. | ||
+ | * Otherwise, if {{c|P}} and {{c|Q}} point to, respectively, the {{tt|i}}th and {{tt|j}}th array elements of the same array object {{c|x}}, the expression {{c|P - Q}} has the value {{c|i − j}}. | ||
+ | :* If {{c|i − j}} is not representable by {{lc|std::ptrdiff_t}}, the behavior is undefined. | ||
+ | * Otherwise, if {{c|P}} and {{c|Q}} point to the same complete object, base class subobject or member subobject, the result is {{c|0}}. | ||
+ | * Otherwise, the behavior is undefined. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
These pointer arithmetic operators allow pointers to satisfy the {{named req|RandomAccessIterator}} requirements. | These pointer arithmetic operators allow pointers to satisfy the {{named req|RandomAccessIterator}} requirements. | ||
+ | For addition and subtraction, if {{c|P}} or {{c|Q}} have type “pointer to (possibly cv-qualified) {{tt|T}}”, where {{tt|T}} and the array element type are not {{rlp|implicit conversion#Similar types|similar}}, the behavior is undefined: | ||
+ | {{source|1= | ||
+ | int arr[5] = {1, 2, 3, 4, 5}; | ||
+ | unsigned int *p = reinterpret_cast<unsigned int*>(arr + 1); | ||
+ | unsigned int k = *p; // OK, the value of “k” is 2 | ||
+ | unsigned int *q = p + 1; // undefined behavior: “p” points to int, not unsigned int | ||
+ | }} | ||
+ | |||
+ | {{anchor|Overloads of additive operators}} | ||
+ | ====Overloads==== | ||
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted arithmetic types {{tt|L}} and {{tt|R}} and for every object type {{tt|T}}, the following function signatures participate in overload resolution: | In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted arithmetic types {{tt|L}} and {{tt|R}} and for every object type {{tt|T}}, the following function signatures participate in overload resolution: | ||
{{dcl begin}} | {{dcl begin}} | ||
Line 235: | Line 281: | ||
{{dcl|std::ptrdiff_t operator-(T*, T*)}} | {{dcl|std::ptrdiff_t operator-(T*, T*)}} | ||
{{dcl end}} | {{dcl end}} | ||
− | where {{tt|LR}} is the result of usual arithmetic conversions on {{tt|L}} and {{tt|R}} | + | where {{tt|LR}} is the result of {{rlp|usual arithmetic conversions}} on {{tt|L}} and {{tt|R}}. |
{{example | {{example | ||
− | |||
|code= | |code= | ||
#include <iostream> | #include <iostream> | ||
Line 246: | Line 291: | ||
char c = 2; | char c = 2; | ||
unsigned int un = 2; | unsigned int un = 2; | ||
− | int | + | int n = -10; |
− | std::cout << | + | std::cout << " 2 + (-10), where 2 is a char = " << c + n << "\n" |
− | + | " 2 + (-10), where 2 is unsigned = " << un + n << "\n" | |
− | + | " -10 - 2.12 = " << n - 2.12 << '\n'; | |
char a[4] = {'a', 'b', 'c', 'd'}; | char a[4] = {'a', 'b', 'c', 'd'}; | ||
Line 267: | Line 312: | ||
====Multiplicative operators==== | ====Multiplicative operators==== | ||
− | The | + | The multiplicative operator expressions have the form |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 274: | Line 319: | ||
{{sdsc|num=3|{{spar|lhs}} {{ttb|%}} {{spar|rhs}}}} | {{sdsc|num=3|{{spar|lhs}} {{ttb|%}} {{spar|rhs}}}} | ||
{{sdsc end}} | {{sdsc end}} | ||
− | @1@ | + | @1@ Multiplication. |
− | + | @2@ Division. | |
− | @2@ | + | @3@ Remainder. |
− | + | ||
− | @3@ | + | |
− | + | ||
− | + | Multiplicative operators have higher {{rlp|operator precedence|precedence}} than all other binary arithmetic operators. These operators associate from left to right: | |
+ | {{source| | ||
+ | a + b * c; // equivalent to a + (b * c), NOT (a + b) * c | ||
+ | d / e - f; // equivalent to (d / e) - f, NOT d / (e - f) | ||
+ | g % h >> i; // equivalent to (g % h) >> i, NOT g % (h >> i) | ||
− | + | j * k / l % m; // equivalent to ((j * k) / l) % m | |
− | + | }} | |
− | + | ||
+ | ====Built-in multiplicative operators==== | ||
+ | For built-in multiplication and division operators, both operands must have arithmetic or unscoped enumeration type. For the built-in remainder operator, both operands must have integral or unscoped enumeration type. {{rlp|usual arithmetic conversions|Usual arithmetic conversions}} are performed on both operands. | ||
− | + | In the remaining description in this section, "operand(s)", {{spar|lhs}} and {{spar|rhs}} refer to the converted operand(s). | |
− | + | @1@ The result of built-in multiplication is the product of the operands. | |
− | + | @@ If both operands have a floating-point type, and the type supports IEEE floating-point arithmetic (see {{lc|std::numeric_limits::is_iec559}}): | |
+ | * Multiplication of a NaN by any number gives NaN. | ||
+ | * Multiplication of infinity by zero gives NaN and {{lc|FE_INVALID}} is raised. | ||
− | If | + | @2@ The result of built-in division is {{spar|lhs}} divided by {{spar|rhs}}. If {{spar|rhs}} is zero, the behavior is undefined. |
− | * | + | @@ If both operands have an integral type, the result is the algebraic quotient (performs integer division): the quotient is truncated towards zero (fractional part is discarded). |
− | * | + | @@ If both operands have a floating-point type, and the type supports IEEE floating-point arithmetic (see {{lc|std::numeric_limits::is_iec559}}): |
− | * | + | * If one operand is NaN, the result is NaN. |
+ | * Dividing a non-zero number by ±0.0 gives the correctly-signed infinity and {{lc|FE_DIVBYZERO}} is raised. | ||
+ | * Dividing 0.0 by 0.0 gives NaN and {{lc|FE_INVALID}} is raised. | ||
− | The | + | @3@ The result of built-in remainder is the remainder of the integer division of {{spar|lhs}} by {{spar|rhs}}. If {{spar|rhs}} is zero, the behavior is undefined. |
+ | @@ If {{c|a / b}} is representable in the result type, {{c|1=(a / b) * b + a % b == a}}. | ||
+ | @@ If {{c|a / b}} is not representable in the result type, the behavior of both {{c|a / b}} and {{c|a % b}} is undefined (that means {{c|INT_MIN % -1}} is undefined on two's complement systems). | ||
Note: Until {{cwg|614}} was resolved ({{stddoc|n2757}}), if one or both operands to binary operator % were negative, the sign of the remainder was implementation-defined, as it depends on the rounding direction of integer division. The function {{lc|std::div}} provided well-defined behavior in that case. | Note: Until {{cwg|614}} was resolved ({{stddoc|n2757}}), if one or both operands to binary operator % were negative, the sign of the remainder was implementation-defined, as it depends on the rounding direction of integer division. The function {{lc|std::div}} provided well-defined behavior in that case. | ||
Line 303: | Line 357: | ||
Note: for floating-point remainder, see {{lc|std::remainder}} and {{lc|std::fmod}}. | Note: for floating-point remainder, see {{lc|std::remainder}} and {{lc|std::fmod}}. | ||
+ | {{anchor|Overloads of multiplicative operators}} | ||
+ | ====Overloads==== | ||
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted arithmetic types {{tt|LA}} and {{tt|RA}} and for every pair of promoted integral types {{tt|LI}} and {{tt|RI}} the following function signatures participate in overload resolution: | In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted arithmetic types {{tt|LA}} and {{tt|RA}} and for every pair of promoted integral types {{tt|LI}} and {{tt|RI}} the following function signatures participate in overload resolution: | ||
{{dcl begin}} | {{dcl begin}} | ||
Line 309: | Line 365: | ||
{{dcl|LRI operator%(LI, RI)}} | {{dcl|LRI operator%(LI, RI)}} | ||
{{dcl end}} | {{dcl end}} | ||
− | where {{tt|LRx}} is the result of usual arithmetic conversions on {{tt|Lx}} and {{tt|Rx}} | + | where {{tt|LRx}} is the result of {{rlp|usual arithmetic conversions}} on {{tt|Lx}} and {{tt|Rx}}. |
{{example | {{example | ||
− | |||
|code= | |code= | ||
#include <iostream> | #include <iostream> | ||
Line 321: | Line 376: | ||
unsigned int un = 2; | unsigned int un = 2; | ||
int n = -10; | int n = -10; | ||
− | std::cout << | + | std::cout << "2 * (-10), where 2 is a char = " << c * n << "\n" |
− | + | "2 * (-10), where 2 is unsigned = " << un * n << "\n" | |
− | + | "-10 / 2.12 = " << n / 2.12 << "\n" | |
− | + | "-10 / 21 = " << n / 21 << "\n" | |
− | + | "-10 % 21 = " << n % 21 << '\n'; | |
} | } | ||
|output= | |output= | ||
Line 335: | Line 390: | ||
}} | }} | ||
− | + | ===Bitwise logic operators=== | |
− | The bitwise | + | The bitwise logic operator expressions have the form |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 344: | Line 399: | ||
{{sdsc|num=4|{{spar|lhs}} {{ttb|^}} {{spar|rhs}}}} | {{sdsc|num=4|{{spar|lhs}} {{ttb|^}} {{spar|rhs}}}} | ||
{{sdsc end}} | {{sdsc end}} | ||
− | @1@ | + | @1@ Bitwise NOT. |
− | @2@ | + | @2@ Bitwise AND. |
− | @3@ | + | @3@ Bitwise OR. |
− | @4@ | + | @4@ Bitwise XOR. |
− | + | ||
− | The | + | The bitwise NOT operator has higher {{rlp|operator precedence|precedence}} than all binary arithmetic operators. It associates from right to left: |
+ | {{source| | ||
+ | ~a - b; // equivalent to (~a) - b, NOT ~(a - b) | ||
+ | ~c * d; // equivalent to (~c) * d, NOT ~(c * d) | ||
+ | ~-e; // equivalent to ~(-e) | ||
+ | }} | ||
+ | |||
+ | There is an ambiguity in the grammar when {{c|~}} is followed by a {{rlp|type#type naming|type name}}{{rev inl|since=c++11| or {{rlpt|decltype}} specifier}}: it can either be operator~ or start a {{rlp|destructor}} identifier). The ambiguity is resolved by treating {{c|~}} as operator~. {{c|~}} can start a destructor identifier only in places where forming an operator~ is syntactically invalid. | ||
+ | |||
+ | All other bitwise logic operators have lower {{rlp|operator precedence|precedence}} than all other binary arithmetic operators. Bitwise AND has higher precedence than bitwise XOR, which has higher precedence than bitwise OR. They associate from left to right: | ||
+ | {{source| | ||
+ | a & b * c; // equivalent to a & (b * c), NOT (a & b) * c | ||
+ | d / e ^ f; // equivalent to (d / e) ^ f, NOT d / (e ^ f) | ||
+ | g << h {{!}} i; // equivalent to (g << h) {{!}} i, NOT g << (h {{!}} i) | ||
+ | |||
+ | j & k & l; // equivalent to (j & k) & l | ||
+ | m {{!}} n ^ o // equivalent to m {{!}} (n ^ o) | ||
+ | }} | ||
+ | |||
+ | ====Built-in bitwise logic operators==== | ||
+ | For the built-in bitwise NOT operator, {{spar|rhs}} must be a prvalue of integral or unscoped enumeration type, and integral promotion is performed on {{spar|rhs}}. For other built-in bitwise logic operators, both operands must have integral or unscoped enumeration type, and {{rlp|usual arithmetic conversions}} are performed on both operands. | ||
+ | |||
+ | In the remaining description in this section, "operand(s)", {{spar|lhs}} and {{spar|rhs}} refer to the converted or promoted operand(s). | ||
+ | |||
+ | @1@ Given the operand as {{c|x}} and the result of the built-in bitwise NOT operation as {{c|r}}. For each coefficient {{c|x_i}} of the base-2 representation of {{c|x}}, the corresponding coefficient {{c|r_i}} of the base-2 representation of {{c|r}} is {{c|1}} if {{c|x_i}} is {{c|0}}, and {{c|0}} otherwise. | ||
+ | * In other words, the result is the one’s complement of the operand (where operand and result are considered as unsigned). | ||
+ | @@ The type of the result {{c|r}} is the type of the operand {{c|x}}. | ||
+ | |||
+ | @2-4@ Given the operands as {{c|x}} and {{c|y}} respectively and the result of the built-in binary bitwise logic operations as {{c|r}}. For each pair of coefficients {{c|x_i}} and {{c|y_i}} of the base-2 representations of {{c|x}} and {{c|y}} respectively, the corresponding coefficient {{c|r_i}} of the base-2 representation of {{c|r}} is | ||
+ | :@2@ {{c|1}} if both {{c|x_i}} and {{c|y_i}} are {{c|1}}, and {{c|0}} otherwise. | ||
+ | :@3@ {{c|1}} if at least one of {{c|x_i}} and {{c|y_i}} is {{c|1}}, and {{c|0}} otherwise. | ||
+ | :@4@ {{c|1}} if either (but not both) of {{c|x_i}} and {{c|y_i}} is {{c|1}}, and {{c|0}} otherwise. | ||
+ | @@ The type of the result {{c|r}} is the type of the operands {{c|x}} and {{c|y}}. | ||
+ | |||
+ | {{anchor|Overloads of bitwise logic operators}} | ||
+ | ====Overloads==== | ||
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted integral types {{tt|L}} and {{tt|R}} the following function signatures participate in overload resolution: | In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted integral types {{tt|L}} and {{tt|R}} the following function signatures participate in overload resolution: | ||
{{dcl begin}} | {{dcl begin}} | ||
Line 359: | Line 448: | ||
{{dcl|LR operator{{!}}(L, R)}} | {{dcl|LR operator{{!}}(L, R)}} | ||
{{dcl end}} | {{dcl end}} | ||
− | where {{tt|LR}} is the result of usual arithmetic conversions on {{tt|L}} and {{tt|R}} | + | where {{tt|LR}} is the result of {{rlp|usual arithmetic conversions}} on {{tt|L}} and {{tt|R}}. |
{{example | {{example | ||
− | |||
|code= | |code= | ||
− | |||
− | |||
#include <bitset> | #include <bitset> | ||
+ | #include <cstdint> | ||
+ | #include <iomanip> | ||
+ | #include <iostream> | ||
int main() | int main() | ||
{ | { | ||
− | uint16_t mask = 0x00f0; | + | std::uint16_t mask = 0x00f0; |
− | uint32_t x0 = 0x12345678; | + | std::uint32_t x0 = 0x12345678; |
− | uint32_t x1 = x0 {{!}} mask; | + | std::uint32_t x1 = x0 {{!}} mask; |
− | uint32_t x2 = x0 & ~mask; | + | std::uint32_t x2 = x0 & ~mask; |
− | uint32_t x3 = x0 & mask; | + | std::uint32_t x3 = x0 & mask; |
− | uint32_t x4 = x0 ^ mask; | + | std::uint32_t x4 = x0 ^ mask; |
− | uint32_t x5 = ~x0; | + | std::uint32_t x5 = ~x0; |
using bin16 = std::bitset<16>; | using bin16 = std::bitset<16>; | ||
using bin32 = std::bitset<32>; | using bin32 = std::bitset<32>; | ||
std::cout << std::hex << std::showbase | std::cout << std::hex << std::showbase | ||
− | << "Mask: " << mask << std::setw(49) << bin16(mask) << | + | << "Mask: " << mask << std::setw(49) << bin16(mask) << "\n" |
− | + | "Value: " << x0 << std::setw(42) << bin32(x0) << "\n" | |
− | + | "Setting bits: " << x1 << std::setw(35) << bin32(x1) << "\n" | |
− | + | "Clearing bits: " << x2 << std::setw(34) << bin32(x2) << "\n" | |
− | + | "Selecting bits: " << x3 << std::setw(39) << bin32(x3) << "\n" | |
− | + | "XOR-ing bits: " << x4 << std::setw(35) << bin32(x4) << "\n" | |
− | + | "Inverting bits: " << x5 << std::setw(33) << bin32(x5) << '\n'; | |
} | } | ||
|output= | |output= | ||
Line 398: | Line 487: | ||
}} | }} | ||
− | + | ===Bitwise shift operators=== | |
− | The bitwise shift operator expressions have the form | + | The bitwise shift operator expressions have the form |
{{sdsc begin}} | {{sdsc begin}} | ||
Line 405: | Line 494: | ||
{{sdsc|num=2|{{spar|lhs}} {{ttb|>>}} {{spar|rhs}}}} | {{sdsc|num=2|{{spar|lhs}} {{ttb|>>}} {{spar|rhs}}}} | ||
{{sdsc end}} | {{sdsc end}} | ||
− | @1@ left shift | + | @1@ Bitwise left-shift. |
− | @2@ right shift | + | @2@ Bitwise right-shift. |
− | + | ||
+ | Bitwise shift operators have higher {{rlp|operator precedence|precedence}} than bitwise logic operators, but have lower precedence than additive and multiplicative operators. These operators associate from left to right: | ||
+ | {{source| | ||
+ | a >> b * c; // equivalent to a >> (b * c), NOT (a >> b) * c | ||
+ | d << e & f; // equivalent to (d << e) & f, NOT d << (e & f) | ||
+ | |||
+ | g << h >> i; // equivalent to (g << h) >> i, NOT g << (h >> i) | ||
+ | }} | ||
+ | |||
+ | ====Built-in bitwise shift operators==== | ||
+ | For the built-in bitwise shift operators, both operands must be prvalues of integral or unscoped enumeration type. Integral promotions are performed on both operands. | ||
+ | |||
+ | In the remaining description in this section, "operand(s)", {{c|a}}, {{c|b}}, {{spar|lhs}} and {{spar|rhs}} refer to the converted or promoted operand(s). | ||
− | + | If the value of {{spar|rhs}} is negative or is not less than the number of bits in {{spar|lhs}}, the behavior is undefined. | |
{{rrev multi|until1=c++20|rev1= | {{rrev multi|until1=c++20|rev1= | ||
− | For unsigned {{c|a}}, the value of {{c|a << b}} is the value of {{math|a * 2{{su|p=b}}}}, reduced modulo {{math|2{{su|p=N}}}} where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded). | + | For unsigned {{c|a}}, the value of {{c|a << b}} is the value of {{math|a * 2{{su|p=b}}}}, reduced modulo {{math|2{{su|p=N}}}} where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded). |
For signed and non-negative {{c|a}}, if {{math|a * 2{{su|p=b}}}} is representable in the unsigned version of the return type, then that value, {{rlp|implicit conversion#Integral conversions|converted}} to signed, is the value of {{c|a << b}} (this makes it legal to create {{lc|INT_MIN}} as {{c|1 << 31}}); otherwise the behavior is undefined. | For signed and non-negative {{c|a}}, if {{math|a * 2{{su|p=b}}}} is representable in the unsigned version of the return type, then that value, {{rlp|implicit conversion#Integral conversions|converted}} to signed, is the value of {{c|a << b}} (this makes it legal to create {{lc|INT_MIN}} as {{c|1 << 31}}); otherwise the behavior is undefined. | ||
− | For negative {{c|a}}, the behavior of {{c|a << b}} is undefined. | + | For negative {{c|a}}, the behavior of {{c|a << b}} is undefined. |
For unsigned {{c|a}} and for signed and non-negative {{c|a}}, the value of {{c|a >> b}} is the integer part of {{math|a/2{{su|p=b}}}}. | For unsigned {{c|a}} and for signed and non-negative {{c|a}}, the value of {{c|a >> b}} is the integer part of {{math|a/2{{su|p=b}}}}. | ||
Line 424: | Line 525: | ||
The value of {{c|a << b}} is the unique value congruent to {{math|a * 2{{su|p=b}}}} modulo {{math|2{{su|p=N}}}} where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded). | The value of {{c|a << b}} is the unique value congruent to {{math|a * 2{{su|p=b}}}} modulo {{math|2{{su|p=N}}}} where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded). | ||
− | The value of {{c|a >> b}} is {{math|a/2{{su|p=b}}}}, rounded | + | The value of {{c|a >> b}} is {{math|a/2{{su|p=b}}}}, rounded towards negative infinity (in other words, right shift on signed {{c|a}} is arithmetic right shift). |
}} | }} | ||
− | + | The type of the result is that of {{spar|lhs}}. | |
+ | {{anchor|Overloads of bitwise shift operators}} | ||
+ | ====Overloads==== | ||
In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted integral types {{tt|L}} and {{tt|R}}, the following function signatures participate in overload resolution: | In {{rlp|overload resolution#Call to an overloaded operator|overload resolution against user-defined operators}}, for every pair of promoted integral types {{tt|L}} and {{tt|R}}, the following function signatures participate in overload resolution: | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl|L operator<<(L, R)}} | {{dcl|L operator<<(L, R)}} | ||
− | {{dcl|L operator>>(L, R)}} | + | {{dcl|L operator>>(L, R)}} |
{{dcl end}} | {{dcl end}} | ||
{{example | {{example | ||
− | |||
|code= | |code= | ||
#include <iostream> | #include <iostream> | ||
Line 447: | Line 549: | ||
char c = 0x10; | char c = 0x10; | ||
unsigned long long ull = 0x123; | unsigned long long ull = 0x123; | ||
− | std::cout << "0x123 << 1 = " << (ull << 1) << | + | std::cout << "0x123 << 1 = " << (ull << 1) << "\n" |
− | + | "0x123 << 63 = " << (ull << 63) << "\n" // overflow in unsigned | |
− | + | "0x10 << 10 = " << (c << 10) << '\n'; // char is promoted to int | |
long long ll = -1000; | long long ll = -1000; | ||
std::cout << std::dec << "-1000 >> 1 = " << (ll >> ONE) << '\n'; | std::cout << std::dec << "-1000 >> 1 = " << (ll >> ONE) << '\n'; | ||
Line 465: | Line 567: | ||
====Unary arithmetic operators==== | ====Unary arithmetic operators==== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc|cpp/chrono/duration/dsc | + | {{dsc inc|cpp/chrono/duration/dsc operator arith}} |
− | {{dsc inc|cpp/numeric/complex/dsc | + | {{dsc inc|cpp/numeric/complex/dsc operator arith2}} |
− | {{dsc inc|cpp/numeric/valarray/dsc | + | {{dsc inc|cpp/numeric/valarray/dsc operator arith}} |
{{dsc end}} | {{dsc end}} | ||
====Additive operators==== | ====Additive operators==== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc|cpp/chrono/time_point/dsc | + | {{dsc inc|cpp/chrono/time_point/dsc operator arith2}} |
− | {{dsc inc|cpp/chrono/duration/dsc | + | {{dsc inc|cpp/chrono/duration/dsc operator arith4}} |
{{dsc inc|cpp/chrono/year_month_day/dsc operator arith 2}} | {{dsc inc|cpp/chrono/year_month_day/dsc operator arith 2}} | ||
{{dsc inc|cpp/string/basic_string/dsc operator+}} | {{dsc inc|cpp/string/basic_string/dsc operator+}} | ||
− | {{dsc mem fun|cpp/iterator/reverse_iterator/ | + | {{dsc mem fun|cpp/iterator/reverse_iterator/operator arith|title=operator+<br>operator-|advances or decrements the iterator}} |
− | {{dsc mem fun|cpp/iterator/move_iterator/ | + | {{dsc mem fun|cpp/iterator/move_iterator/operator arith|title=operator+<br>operator-|advances or decrements the iterator}} |
− | {{dsc inc|cpp/numeric/complex/dsc | + | {{dsc inc|cpp/numeric/complex/dsc operator arith3}} |
− | {{dsc inc|cpp/numeric/valarray/dsc | + | {{dsc inc|cpp/numeric/valarray/dsc operator arith3}} |
{{dsc end}} | {{dsc end}} | ||
====Multiplicative operators==== | ====Multiplicative operators==== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc|cpp/chrono/duration/dsc | + | {{dsc inc|cpp/chrono/duration/dsc operator arith4}} |
− | {{dsc inc|cpp/numeric/complex/dsc | + | {{dsc inc|cpp/numeric/complex/dsc operator arith3}} |
− | {{dsc inc|cpp/numeric/valarray/dsc | + | {{dsc inc|cpp/numeric/valarray/dsc operator arith3}} |
{{dsc end}} | {{dsc end}} | ||
====Bitwise logic operators==== | ====Bitwise logic operators==== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc|cpp/utility/bitset/dsc | + | {{dsc inc|cpp/utility/bitset/dsc operator logic}} |
− | {{dsc inc|cpp/utility/bitset/dsc | + | {{dsc inc|cpp/utility/bitset/dsc operator logic2}} |
− | {{dsc mem fun|cpp/numeric/valarray/ | + | {{dsc mem fun|cpp/numeric/valarray/operator arith|title=operator~|applies a unary arithmetic operator to each element of the valarray}} |
− | {{dsc tfun|cpp/numeric/valarray/ | + | {{dsc tfun|cpp/numeric/valarray/operator arith3|title=operator^<br>operator&<br>operator{{!}}|applies binary operators to each element of two valarrays, or a valarray and a value}} |
{{dsc end}} | {{dsc end}} | ||
====Bitwise shift operators==== | ====Bitwise shift operators==== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc tfun|cpp/numeric/valarray/ | + | {{dsc tfun|cpp/numeric/valarray/operator arith3|title=operator<<<br>operator>>|applies binary operators to each element of two valarrays, or a valarray and a value}} |
− | {{dsc mem fun|cpp/utility/bitset/ | + | {{dsc mem fun|cpp/utility/bitset/operator ltltgtgt|title=operator<<<br>operator>>|performs binary shift left and shift right}} |
{{dsc end}} | {{dsc end}} | ||
====Stream insertion/extraction operators==== | ====Stream insertion/extraction operators==== | ||
− | Throughout the standard library, bitwise shift operators are commonly overloaded with I/O stream ({{c|std::ios_base&}} or one of the classes derived from it) as both the left operand and return type. Such operators are known as ''stream insertion'' and ''stream extraction'' operators: | + | Throughout the standard library, bitwise shift operators are commonly overloaded with I/O stream ({{c/core|std::ios_base&}} or one of the classes derived from it) as both the left operand and return type. Such operators are known as ''stream insertion'' and ''stream extraction'' operators: |
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc|cpp/io/basic_istream/dsc | + | {{dsc inc|cpp/io/basic_istream/dsc operator gtgt}} |
− | {{dsc inc|cpp/io/basic_istream/dsc | + | {{dsc inc|cpp/io/basic_istream/dsc operator gtgt2}} |
− | {{dsc inc|cpp/io/basic_ostream/dsc | + | {{dsc inc|cpp/io/basic_ostream/dsc operator ltlt}} |
− | {{dsc inc|cpp/io/basic_ostream/dsc | + | {{dsc inc|cpp/io/basic_ostream/dsc operator ltlt2}} |
− | {{dsc inc|cpp/numeric/complex/dsc | + | {{dsc inc|cpp/numeric/complex/dsc operator ltltgtgt}} |
− | {{dsc inc|cpp/utility/bitset/dsc | + | {{dsc inc|cpp/utility/bitset/dsc operator ltltgtgt2}} |
− | {{dsc inc|cpp/string/basic_string/dsc | + | {{dsc inc|cpp/string/basic_string/dsc operator ltltgtgt}} |
− | {{dsc inc|cpp/numeric/random/engine/dsc | + | {{dsc inc|cpp/numeric/random/engine/dsc operator ltltgtgt|linear_congruential_engine}} |
− | {{dsc inc|cpp/numeric/random/distribution/dsc | + | {{dsc inc|cpp/numeric/random/distribution/dsc operator ltltgtgt|uniform_int_distribution}} |
{{dsc end}} | {{dsc end}} | ||
Line 523: | Line 625: | ||
{{dr list item|wg=cwg|dr=1450|std=C++98|before=the result of {{c|a / b}} was unspecified if<br>it is not representable in the result type|after=the behavior of both {{c|a / b}} and<br>{{c|a % b}} is undefined in this case}} | {{dr list item|wg=cwg|dr=1450|std=C++98|before=the result of {{c|a / b}} was unspecified if<br>it is not representable in the result type|after=the behavior of both {{c|a / b}} and<br>{{c|a % b}} is undefined in this case}} | ||
{{dr list item|wg=cwg|dr=1457|std=C++98|before=the behavior of shifting the leftmost {{tt|1}} bit of a<br>positive signed value into the sign bit was undefined|after=made well-defined}} | {{dr list item|wg=cwg|dr=1457|std=C++98|before=the behavior of shifting the leftmost {{tt|1}} bit of a<br>positive signed value into the sign bit was undefined|after=made well-defined}} | ||
+ | {{dr list item|wg=cwg|dr=1504|std=C++98|before=a pointer to a base class subobject of an array<br>element could be used in pointer arithmetic|after=the behavior is<br>undefined in this case}} | ||
+ | {{dr list item|wg=cwg|dr=1515|std=C++98|before=only unsigned integers which declared {{c/core|unsigned}} <br>should obey the laws of arithmetic {{math|modulo 2{{su|p=n}}}}|after=applies to all unsigned integers}} | ||
+ | {{dr list item|wg=cwg|dr=1642|std=C++98|before=arithmetic operators allow their operands to be lvalues|after=some operands must be rvalues}} | ||
+ | {{dr list item|wg=cwg|dr=1865|std=C++98|before=the resolution of {{cwg|1504}} made the behaviors<br>of pointer arithmetic involving pointers to array element<br>undefined if the pointed-to type and the array element<br>type have different cv-qualifications in non-top levels|after=made well-defined}} | ||
+ | {{dr list item|wg=cwg|dr=1971|std=C++98|before=it was unclear whether the rule resolving the<br>ambiguity of {{c|~}} applies to cases such as {{c|~X(0)}}|after=the rule applies to such cases}} | ||
+ | {{dr list item|wg=cwg|dr=2419|std=C++98|before=a pointer to non-array object was only treated as a<br>pointer to the first element of an array with size 1<br>in pointer arithmetic if the pointer is obtained by {{c|&}}|after=applies to all pointers<br>to non-array objects}} | ||
+ | {{dr list item|wg=cwg|dr=2626|std=C++98|before=the result of built-in {{tt|operator~}} was simply<br>'one's complement' without proper definition|after=the result is phrased in terms<br>of the base-2 representation}} | ||
+ | {{dr list item|wg=cwg|dr=2724|std=C++20|before=the rounding direction of arithmetic right shift was unclear|after=made clear}} | ||
+ | {{dr list item|wg=cwg|dr=2853|std=C++98|before=a pointer past the end of an object could<br>not be added or subtracted with an integer|after=it can}} | ||
{{dr list end}} | {{dr list end}} | ||
Latest revision as of 23:30, 24 June 2024
Returns the result of specific arithmetic operation.
Operator name | Syntax | Prototype examples (for class T) | ||
---|---|---|---|---|
Inside class definition | Outside class definition | |||
Unary plus | +a | T T::operator+() const; | T operator+(const T& a); | |
Unary minus | -a | T T::operator-() const; | T operator-(const T& a); | |
Addition | a + b | T T::operator+(const T2& b) const; | T operator+(const T& a, const T2& b); | |
Subtraction | a - b | T T::operator-(const T2& b) const; | T operator-(const T& a, const T2& b); | |
Multiplication | a * b | T T::operator*(const T2& b) const; | T operator*(const T& a, const T2& b); | |
Division | a / b | T T::operator/(const T2& b) const; | T operator/(const T& a, const T2& b); | |
Remainder | a % b | T T::operator%(const T2& b) const; | T operator%(const T& a, const T2& b); | |
Bitwise NOT | ~a | T T::operator~() const; | T operator~(const T& a); | |
Bitwise AND | a & b | T T::operator&(const T2& b) const; | T operator&(const T& a, const T2& b); | |
Bitwise OR | a | b | T T::operator|(const T2& b) const; | T operator|(const T& a, const T2& b); | |
Bitwise XOR | a ^ b | T T::operator^(const T2& b) const; | T operator^(const T& a, const T2& b); | |
Bitwise left shift | a << b | T T::operator<<(const T2& b) const; | T operator<<(const T& a, const T2& b); | |
Bitwise right shift | a >> b | T T::operator>>(const T2& b) const; | T operator>>(const T& a, const T2& b); | |
|
Contents |
[edit] General explanation
All built-in arithmetic operators compute the result of specific arithmetic operation and returns its result. The arguments are not modified.
[edit] Conversions
If the operand passed to a built-in arithmetic operator is integral or unscoped enumeration type, then before any other action (but after lvalue-to-rvalue conversion, if applicable), the operand undergoes integral promotion. If an operand has array or function type, array-to-pointer and function-to-pointer conversions are applied.
For the binary operators (except shifts), if the promoted operands have different types, usual arithmetic conversions are applied.
[edit] Overflows
Unsigned integer arithmetic is always performed modulo 2n where n is the number of bits in that particular integer. E.g. for unsigned int, adding one to UINT_MAX gives 0, and subtracting one from 0 gives UINT_MAX.
When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined, — the possible manifestations of such an operation include:
- it wraps around according to the rules of the representation (typically two's complement),
- it traps — on some platforms or due to compiler options (e.g.
-ftrapv
in GCC and Clang), - it saturates to minimal or maximal value (on many DSPs),
- it is completely optimized out by the compiler.
[edit] Floating-point environment
If
#pragma STDC FENV_ACCESS is supported and set to ON
, all floating-point arithmetic operators obey the current floating-point rounding direction and report floating-point arithmetic errors as specified in math_errhandling unless part of a static initializer (in which case floating-point exceptions are not raised and the rounding mode is to nearest).
[edit] Floating-point contraction
Unless
#pragma STDC FP_CONTRACT is supported and set to OFF
, all floating-point arithmetic may be performed as if the intermediate results have infinite range and precision, that is, optimizations that omit rounding errors and floating-point exceptions are allowed. For example, C++ allows the implementation of (x * y) + z with a single fused multiply-add CPU instruction or optimization of a = x * x * x * x; as tmp = x * x; a = tmp * tmp.
Unrelated to contracting, intermediate results of floating-point arithmetic may have range and precision that is different from the one indicated by its type, see FLT_EVAL_METHOD.
Formally, the C++ standard makes no guarantee on the accuracy of floating-point operations.
[edit] Unary arithmetic operators
The unary arithmetic operator expressions have the form
+ expression
|
(1) | ||||||||
- expression
|
(2) | ||||||||
Unary +
and -
operators have higher precedence than all binary arithmetic operators, so expression cannot contain top-level binary arithmetic operators. These operators associate from right to left:
+a - b; // equivalent to (+a) - b, NOT +(a - b) -c + d; // equivalent to (-c) + d, NOT -(c + d) +-e; // equivalent to +(-e), the unary + is a no-op if “e” is a built-in type // because any possible promotion is performed during negation already
[edit] Built-in unary arithmetic operators
- In other words, the result is the two’s complement of the operand (where operand and result are considered as unsigned).
[edit] Overloads
In overload resolution against user-defined operators, for every cv-unqualified promoted arithmetic type A
and for every type T
, the following function signatures participate in overload resolution:
A operator+(A) |
||
T* operator+(T*) |
||
A operator-(A) |
||
#include <iostream> int main() { char c = 0x6a; int n1 = 1; unsigned char n2 = 1; unsigned int n3 = 1; std::cout << "char: " << c << " int: " << +c << "\n" "-1, where 1 is signed: " << -n1 << "\n" "-1, where 1 is unsigned char: " << -n2 << "\n" "-1, where 1 is unsigned int: " << -n3 << '\n'; char a[3]; std::cout << "size of array: " << sizeof a << "\n" "size of pointer: " << sizeof +a << '\n'; }
Possible output:
char: j int: 106 -1, where 1 is signed: -1 -1, where 1 is unsigned char: -1 -1, where 1 is unsigned int: 4294967295 size of array: 3 size of pointer: 8
[edit] Additive operators
The additive operator expressions have the form
lhs + rhs
|
(1) | ||||||||
lhs - rhs
|
(2) | ||||||||
Binary +
and -
operators have higher precedence than all other binary arithmetic operators except *
, /
and %
. These operators associate from left to right:
a + b * c; // equivalent to a + (b * c), NOT (a + b) * c d / e - f; // equivalent to (d / e) - f, NOT d / (e - f) g + h >> i; // equivalent to (g + h) >> i, NOT g + (h >> i) j - k + l - m; // equivalent to ((j - k) + l) - m
[edit] Built-in additive operators
For built-in binary plus and binary minus operators, both of lhs and rhs must be prvalues, and one of the following conditions must be satisfied:
- Both operands have arithmetic or unscoped enumeration type. In this case, usual arithmetic conversions are performed on both operands.
- Exactly one operand has integral or unscoped enumeration type. In this case, integral promotion is applied to that operand.
In the remaining description in this section, "operand(s)", lhs and rhs refer to the converted or promoted operand(s).
- Both operands have arithmetic type. In this case, the result is the sum of the operands.
- One operand is a pointer to a completely-defined object type, and the other operand has integral type. In this case, the integral value is added to the pointer (see pointer arithmetic).
- Both operands have arithmetic type. In this case, the result is the difference resulting from the subtraction of rhs from lhs.
- lhs is a pointer to a completely-defined object type, and rhs has integral type. In this case, the integral value is subtracted from the pointer (see pointer arithmetic).
- Both operands are pointers to cv-qualified or cv-unqualified versions of the same completely-defined object type. In this case rhs is subtracted from lhs (see pointer arithmetic).
If both operands have a floating-point type, and the type supports IEEE floating-point arithmetic (see std::numeric_limits::is_iec559):
- If one operand is NaN, the result is NaN.
- Infinity minus infinity is NaN, and FE_INVALID is raised.
- Infinity plus the negative infinity is NaN, and FE_INVALID is raised.
[edit] Pointer arithmetic
When an expression J that has integral type is added to or subtracted from an expression P of pointer type, the result has the type of P.
- If P evaluates to a null pointer value and J evaluates to 0, the result is a null pointer value.
- Otherwise, if P points to the
i
th element of an array object x with n elements, given the value of J as j, P is added or subtracted as follows:
- The expressions P + J and J + P
- point to the
i+j
th element of x if i + j is in[
0,
n)
, and - are pointers past the end of the last element of x if i + j is n.
- point to the
- The expression P - J
- points to the
i-j
th element of x if i - j is in[
0,
n)
, and - is a pointer past the end of the last element of x if i - j is n.
- points to the
- Other j values result in undefined behavior.
- Otherwise, if P points to a complete object, a base class subobject or a member subobject y, given the value of J as j, P is added or subtracted as follows:
- The expressions P + J and J + P
- point to y if j is 0, and
- are pointers past the end of y if j is 1.
- The expression P - J
- points to y if j is 0, and
- is a pointer past the end of y if j is -1.
- Other j values result in undefined behavior.
- Otherwise, if P is a pointer past the end of an object z, given the value of J as j:
- If z is an array object with n elements, P is added or subtracted as follows:
- The expressions P + J and J + P
- point to the
n+j
th element of z if n + j is in[
0,
n)
, and - are pointers past the end of the last element of z if j is 0.
- point to the
- The expression P - J
- points to the
n-j
th element of z if n - j is in[
0,
n)
, and - is a pointer past the end of the last element of z if j is 0.
- points to the
- Other j values result in undefined behavior.
- Otherwise, P is added or subtracted as follows:
- The expressions P + J and J + P
- point to z if j is -1, and
- are pointers past the end of z if j is 0.
- The expression P - J
- points to z if j is 1, and
- is a pointer past the end of z if j is 0.
- Other j values result in undefined behavior.
- Otherwise, the behavior is undefined.
When two pointer expressions P and Q are subtracted, the type of the result is std::ptrdiff_t.
- If P and Q both evaluate to null pointer values, the result is 0.
- Otherwise, if P and Q point to, respectively, the
i
th andj
th array elements of the same array object x, the expression P - Q has the value i − j.
- If i − j is not representable by std::ptrdiff_t, the behavior is undefined.
- Otherwise, if P and Q point to the same complete object, base class subobject or member subobject, the result is 0.
- Otherwise, the behavior is undefined.
These pointer arithmetic operators allow pointers to satisfy the LegacyRandomAccessIterator requirements.
For addition and subtraction, if P or Q have type “pointer to (possibly cv-qualified) T
”, where T
and the array element type are not similar, the behavior is undefined:
int arr[5] = {1, 2, 3, 4, 5}; unsigned int *p = reinterpret_cast<unsigned int*>(arr + 1); unsigned int k = *p; // OK, the value of “k” is 2 unsigned int *q = p + 1; // undefined behavior: “p” points to int, not unsigned int
[edit] Overloads
In overload resolution against user-defined operators, for every pair of promoted arithmetic types L
and R
and for every object type T
, the following function signatures participate in overload resolution:
LR operator+(L, R) |
||
LR operator-(L, R) |
||
T* operator+(T*, std::ptrdiff_t) |
||
T* operator+(std::ptrdiff_t, T*) |
||
T* operator-(T*, std::ptrdiff_t) |
||
std::ptrdiff_t operator-(T*, T*) |
||
where LR
is the result of usual arithmetic conversions on L
and R
.
#include <iostream> int main() { char c = 2; unsigned int un = 2; int n = -10; std::cout << " 2 + (-10), where 2 is a char = " << c + n << "\n" " 2 + (-10), where 2 is unsigned = " << un + n << "\n" " -10 - 2.12 = " << n - 2.12 << '\n'; char a[4] = {'a', 'b', 'c', 'd'}; char* p = &a[1]; std::cout << "Pointer addition examples: " << *p << *(p + 2) << *(2 + p) << *(p - 1) << '\n'; char* p2 = &a[4]; std::cout << "Pointer difference: " << p2 - p << '\n'; }
Output:
2 + (-10), where 2 is a char = -8 2 + (-10), where 2 is unsigned = 4294967288 -10 - 2.12 = -12.12 Pointer addition examples: bdda Pointer difference: 3
[edit] Multiplicative operators
The multiplicative operator expressions have the form
lhs * rhs
|
(1) | ||||||||
lhs / rhs
|
(2) | ||||||||
lhs % rhs
|
(3) | ||||||||
Multiplicative operators have higher precedence than all other binary arithmetic operators. These operators associate from left to right:
a + b * c; // equivalent to a + (b * c), NOT (a + b) * c d / e - f; // equivalent to (d / e) - f, NOT d / (e - f) g % h >> i; // equivalent to (g % h) >> i, NOT g % (h >> i) j * k / l % m; // equivalent to ((j * k) / l) % m
[edit] Built-in multiplicative operators
For built-in multiplication and division operators, both operands must have arithmetic or unscoped enumeration type. For the built-in remainder operator, both operands must have integral or unscoped enumeration type. Usual arithmetic conversions are performed on both operands.
In the remaining description in this section, "operand(s)", lhs and rhs refer to the converted operand(s).
- Multiplication of a NaN by any number gives NaN.
- Multiplication of infinity by zero gives NaN and FE_INVALID is raised.
- If one operand is NaN, the result is NaN.
- Dividing a non-zero number by ±0.0 gives the correctly-signed infinity and FE_DIVBYZERO is raised.
- Dividing 0.0 by 0.0 gives NaN and FE_INVALID is raised.
Note: Until CWG issue 614 was resolved (N2757), if one or both operands to binary operator % were negative, the sign of the remainder was implementation-defined, as it depends on the rounding direction of integer division. The function std::div provided well-defined behavior in that case.
Note: for floating-point remainder, see std::remainder and std::fmod.
[edit] Overloads
In overload resolution against user-defined operators, for every pair of promoted arithmetic types LA
and RA
and for every pair of promoted integral types LI
and RI
the following function signatures participate in overload resolution:
LRA operator*(LA, RA) |
||
LRA operator/(LA, RA) |
||
LRI operator%(LI, RI) |
||
where LRx
is the result of usual arithmetic conversions on Lx
and Rx
.
#include <iostream> int main() { char c = 2; unsigned int un = 2; int n = -10; std::cout << "2 * (-10), where 2 is a char = " << c * n << "\n" "2 * (-10), where 2 is unsigned = " << un * n << "\n" "-10 / 2.12 = " << n / 2.12 << "\n" "-10 / 21 = " << n / 21 << "\n" "-10 % 21 = " << n % 21 << '\n'; }
Output:
2 * (-10), where 2 is a char = -20 2 * (-10), where 2 is unsigned = 4294967276 -10 / 2.12 = -4.71698 -10 / 21 = 0 -10 % 21 = -10
[edit] Bitwise logic operators
The bitwise logic operator expressions have the form
~ rhs
|
(1) | ||||||||
lhs & rhs
|
(2) | ||||||||
lhs | rhs
|
(3) | ||||||||
lhs ^ rhs
|
(4) | ||||||||
The bitwise NOT operator has higher precedence than all binary arithmetic operators. It associates from right to left:
~a - b; // equivalent to (~a) - b, NOT ~(a - b) ~c * d; // equivalent to (~c) * d, NOT ~(c * d) ~-e; // equivalent to ~(-e)
There is an ambiguity in the grammar when ~ is followed by a type name or decltype
specifier(since C++11): it can either be operator~ or start a destructor identifier). The ambiguity is resolved by treating ~ as operator~. ~ can start a destructor identifier only in places where forming an operator~ is syntactically invalid.
All other bitwise logic operators have lower precedence than all other binary arithmetic operators. Bitwise AND has higher precedence than bitwise XOR, which has higher precedence than bitwise OR. They associate from left to right:
a & b * c; // equivalent to a & (b * c), NOT (a & b) * c d / e ^ f; // equivalent to (d / e) ^ f, NOT d / (e ^ f) g << h | i; // equivalent to (g << h) | i, NOT g << (h | i) j & k & l; // equivalent to (j & k) & l m | n ^ o // equivalent to m | (n ^ o)
[edit] Built-in bitwise logic operators
For the built-in bitwise NOT operator, rhs must be a prvalue of integral or unscoped enumeration type, and integral promotion is performed on rhs. For other built-in bitwise logic operators, both operands must have integral or unscoped enumeration type, and usual arithmetic conversions are performed on both operands.
In the remaining description in this section, "operand(s)", lhs and rhs refer to the converted or promoted operand(s).
- In other words, the result is the one’s complement of the operand (where operand and result are considered as unsigned).
[edit] Overloads
In overload resolution against user-defined operators, for every pair of promoted integral types L
and R
the following function signatures participate in overload resolution:
R operator~(R) |
||
LR operator&(L, R) |
||
LR operator^(L, R) |
||
LR operator|(L, R) |
||
where LR
is the result of usual arithmetic conversions on L
and R
.
#include <bitset> #include <cstdint> #include <iomanip> #include <iostream> int main() { std::uint16_t mask = 0x00f0; std::uint32_t x0 = 0x12345678; std::uint32_t x1 = x0 | mask; std::uint32_t x2 = x0 & ~mask; std::uint32_t x3 = x0 & mask; std::uint32_t x4 = x0 ^ mask; std::uint32_t x5 = ~x0; using bin16 = std::bitset<16>; using bin32 = std::bitset<32>; std::cout << std::hex << std::showbase << "Mask: " << mask << std::setw(49) << bin16(mask) << "\n" "Value: " << x0 << std::setw(42) << bin32(x0) << "\n" "Setting bits: " << x1 << std::setw(35) << bin32(x1) << "\n" "Clearing bits: " << x2 << std::setw(34) << bin32(x2) << "\n" "Selecting bits: " << x3 << std::setw(39) << bin32(x3) << "\n" "XOR-ing bits: " << x4 << std::setw(35) << bin32(x4) << "\n" "Inverting bits: " << x5 << std::setw(33) << bin32(x5) << '\n'; }
Output:
Mask: 0xf0 0000000011110000 Value: 0x12345678 00010010001101000101011001111000 Setting bits: 0x123456f8 00010010001101000101011011111000 Clearing bits: 0x12345608 00010010001101000101011000001000 Selecting bits: 0x70 00000000000000000000000001110000 XOR-ing bits: 0x12345688 00010010001101000101011010001000 Inverting bits: 0xedcba987 11101101110010111010100110000111
[edit] Bitwise shift operators
The bitwise shift operator expressions have the form
lhs << rhs
|
(1) | ||||||||
lhs >> rhs
|
(2) | ||||||||
Bitwise shift operators have higher precedence than bitwise logic operators, but have lower precedence than additive and multiplicative operators. These operators associate from left to right:
a >> b * c; // equivalent to a >> (b * c), NOT (a >> b) * c d << e & f; // equivalent to (d << e) & f, NOT d << (e & f) g << h >> i; // equivalent to (g << h) >> i, NOT g << (h >> i)
[edit] Built-in bitwise shift operators
For the built-in bitwise shift operators, both operands must be prvalues of integral or unscoped enumeration type. Integral promotions are performed on both operands.
In the remaining description in this section, "operand(s)", a, b, lhs and rhs refer to the converted or promoted operand(s).
If the value of rhs is negative or is not less than the number of bits in lhs, the behavior is undefined.
For unsigned a, the value of a << b is the value of a * 2b, reduced modulo 2N where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded). For signed and non-negative a, if a * 2b is representable in the unsigned version of the return type, then that value, converted to signed, is the value of a << b (this makes it legal to create INT_MIN as 1 << 31); otherwise the behavior is undefined. For negative a, the behavior of a << b is undefined. For unsigned a and for signed and non-negative a, the value of a >> b is the integer part of a/2b. For negative a, the value of a >> b is implementation-defined (in most implementations, this performs arithmetic right shift, so that the result remains negative). |
(until C++20) |
The value of a << b is the unique value congruent to a * 2b modulo 2N where N is the number of bits in the return type (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded). The value of a >> b is a/2b, rounded towards negative infinity (in other words, right shift on signed a is arithmetic right shift). |
(since C++20) |
The type of the result is that of lhs.
[edit] Overloads
In overload resolution against user-defined operators, for every pair of promoted integral types L
and R
, the following function signatures participate in overload resolution:
L operator<<(L, R) |
||
L operator>>(L, R) |
||
#include <iostream> enum { ONE = 1, TWO = 2 }; int main() { std::cout << std::hex << std::showbase; char c = 0x10; unsigned long long ull = 0x123; std::cout << "0x123 << 1 = " << (ull << 1) << "\n" "0x123 << 63 = " << (ull << 63) << "\n" // overflow in unsigned "0x10 << 10 = " << (c << 10) << '\n'; // char is promoted to int long long ll = -1000; std::cout << std::dec << "-1000 >> 1 = " << (ll >> ONE) << '\n'; }
Output:
0x123 << 1 = 0x246 0x123 << 63 = 0x8000000000000000 0x10 << 10 = 0x4000 -1000 >> 1 = -500
[edit] Standard library
Arithmetic operators are overloaded for many standard library types.
[edit] Unary arithmetic operators
implements unary + and unary - (public member function of std::chrono::duration<Rep,Period> )
| |
applies unary operators to complex numbers (function template) | |
applies a unary arithmetic operator to each element of the valarray (public member function of std::valarray<T> )
|
[edit] Additive operators
(C++11) |
performs add and subtract operations involving a time point (function template) |
implements arithmetic operations with durations as arguments (function template) | |
(C++20) |
adds or subtracts a year_month_day and some number of years or months (function) |
concatenates two strings, a string and a char, or a string and string_view (function template) | |
advances or decrements the iterator (public member function of std::reverse_iterator<Iter> )
| |
advances or decrements the iterator (public member function of std::move_iterator<Iter> )
| |
performs complex number arithmetic on two complex values or a complex and a scalar (function template) | |
applies binary operators to each element of two valarrays, or a valarray and a value (function template) |
[edit] Multiplicative operators
implements arithmetic operations with durations as arguments (function template) | |
performs complex number arithmetic on two complex values or a complex and a scalar (function template) | |
applies binary operators to each element of two valarrays, or a valarray and a value (function template) |
[edit] Bitwise logic operators
performs binary AND, OR, XOR and NOT (public member function of std::bitset<N> )
| |
performs binary logic operations on bitsets (function template) | |
applies a unary arithmetic operator to each element of the valarray (public member function of std::valarray<T> )
| |
applies binary operators to each element of two valarrays, or a valarray and a value (function template) |
[edit] Bitwise shift operators
applies binary operators to each element of two valarrays, or a valarray and a value (function template) | |
performs binary shift left and shift right (public member function of std::bitset<N> )
|
[edit] Stream insertion/extraction operators
Throughout the standard library, bitwise shift operators are commonly overloaded with I/O stream (std::ios_base& or one of the classes derived from it) as both the left operand and return type. Such operators are known as stream insertion and stream extraction operators:
extracts formatted data (public member function of std::basic_istream<CharT,Traits> )
| |
extracts characters and character arrays (function template) | |
inserts formatted data (public member function of std::basic_ostream<CharT,Traits> )
| |
inserts character data or insert into rvalue stream (function template) | |
serializes and deserializes a complex number (function template) | |
performs stream input and output of bitsets (function template) | |
performs stream input and output on strings (function template) | |
(C++11) |
performs stream input and output on pseudo-random number engine (function template) |
(C++11) |
performs stream input and output on pseudo-random number distribution (function template) |
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 614 | C++98 | the algebraic quotient of integer division was rounded in implementation-defined direction |
the algebraic quotient of integer division is truncated towards zero (fractional part is discarded) |
CWG 1450 | C++98 | the result of a / b was unspecified if it is not representable in the result type |
the behavior of both a / b and a % b is undefined in this case |
CWG 1457 | C++98 | the behavior of shifting the leftmost 1 bit of apositive signed value into the sign bit was undefined |
made well-defined |
CWG 1504 | C++98 | a pointer to a base class subobject of an array element could be used in pointer arithmetic |
the behavior is undefined in this case |
CWG 1515 | C++98 | only unsigned integers which declared unsigned should obey the laws of arithmetic modulo 2n |
applies to all unsigned integers |
CWG 1642 | C++98 | arithmetic operators allow their operands to be lvalues | some operands must be rvalues |
CWG 1865 | C++98 | the resolution of CWG issue 1504 made the behaviors of pointer arithmetic involving pointers to array element undefined if the pointed-to type and the array element type have different cv-qualifications in non-top levels |
made well-defined |
CWG 1971 | C++98 | it was unclear whether the rule resolving the ambiguity of ~ applies to cases such as ~X(0) |
the rule applies to such cases |
CWG 2419 | C++98 | a pointer to non-array object was only treated as a pointer to the first element of an array with size 1 in pointer arithmetic if the pointer is obtained by & |
applies to all pointers to non-array objects |
CWG 2626 | C++98 | the result of built-in operator~ was simply'one's complement' without proper definition |
the result is phrased in terms of the base-2 representation |
CWG 2724 | C++20 | the rounding direction of arithmetic right shift was unclear | made clear |
CWG 2853 | C++98 | a pointer past the end of an object could not be added or subtracted with an integer |
it can |
[edit] See also
Common operators | ||||||
---|---|---|---|---|---|---|
assignment | increment decrement |
arithmetic | logical | comparison | member access |
other |
a = b |
++a |
+a |
!a |
a == b |
a[...] |
function call |
a(...) | ||||||
comma | ||||||
a, b | ||||||
conditional | ||||||
a ? b : c | ||||||
Special operators | ||||||
static_cast converts one type to another related type |
C documentation for Arithmetic operators
|