Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Unary operation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== === Absolute value === Obtaining the [[absolute value]] of a number is a unary operation. This function is defined as <math>|n| = \begin{cases} n, & \mbox{if } n\geq0 \\ -n, & \mbox{if } n<0 \end{cases}</math> where <math>|n|</math> is the absolute value of <math>n</math>. ===Negation=== [[Negation (arithmetic)|Negation]] is used to find the negative value of a single number. Here are some examples: :<math>-(3) = -3</math> :<math>-( -3) = 3</math> ===Factorial=== For any positive integer ''n'', the product of the integers less than or equal to ''n'' is a unary operation called [[factorial]]. In the context of [[complex number]]s, the [[gamma function]] is a unary operation extension of factorial. ===Trigonometry=== In [[trigonometry]], the [[trigonometric functions]], such as <math>\sin</math>, <math>\cos</math>, and <math>\tan</math>, can be seen as unary operations. This is because it is possible to provide only one term as input for these functions and retrieve a result. By contrast, binary operations, such as [[addition]], require two different terms to compute a result. ===Examples from programming languages=== Below is a table summarizing common unary operators along with their symbols, description, and examples:<ref name="summarytable">{{cite web |title=Unary Operators in Programming |url=https://www.geeksforgeeks.org/unary-operators-in-programming/ |website=GeeksforGeeks |access-date=24 April 2024 |date=20 March 2024}}</ref> {| class="wikitable" |- ! Operator !! Symbol !! Description !! Example |- | Increment || <code>++</code> || Increases the value of a variable by 1 || <code>x = 2; ++x; // x is now 3</code> |- | Decrement || <code>β-</code> || Decreases the value of a variable by 1 || <code>y = 10; --y; // y is now 9</code> |- | Unary Plus || <code>+</code> || Indicates a positive value || <code>a = -5; b = +a; // b is -5</code> |- | Unary Minus || <code>-</code> || Indicates a negative value || <code>c = 4; d = -c; // d is -4</code> |- | [[Logical NOT]] || <code>!</code> || Negates the truth value of a Boolean expression || <code>flag = true; result = !flag; // result is false</code> |- | [[Bitwise|Bitwise NOT]] || <code>~</code> || Bitwise negation, flips the bits of an integer || <code>num = 5; result = ~num; // result is -6</code> |} ====JavaScript==== In [[JavaScript]], these operators are unary:<ref>{{cite web |title=Unary Operators |url=https://www.javascripttutorial.net/javascript-unary-operators/}}</ref> *[[Increment and decrement operators|Increment]]: <code>++<span style="color:gray;">x</span></code>, <code><span style="color:gray;">x</span>++</code> *[[Increment and decrement operators|Decrement]]: <code>--<span style="color:gray;">x</span></code>, <code><span style="color:gray;">x</span>--</code> *Positive: <code>+<span style="color:gray;">x</span></code> *Negative: <code>-<span style="color:gray;">x</span></code> *[[Ones' complement]]: <code>~<span style="color:gray;">x</span></code> *[[Negation|Logical negation]]: <code>!<span style="color:gray;">x</span></code> ====C family of languages==== In the [[C (programming language)|C]] family of languages, the following operators are unary:<ref>{{cite book |url=http://www-01.ibm.com/support/docview.wss?uid=swg27002103&aid=1 |page=109 |chapter=5. Expressions and Operators |title=C/C++ Language Reference |version=Version 6.0 |archive-url=https://web.archive.org/web/20121016081612/http://www-01.ibm.com/support/docview.wss?uid=swg27002103&aid=1 |archive-date=2012-10-16}}</ref><ref>{{cite web |url=http://www.sanfoundry.com/c-tutorials-different-unary-operators-operate-operands/ |title=Unary Operators - C Tutorials - Sanfoundry |website=www.sanfoundry.com|date=2 March 2014 }}</ref> *[[Increment and decrement operators|Increment]]: <code>++<span style="color:gray;">x</span></code>, <code><span style="color:gray;">x</span>++</code> *[[Increment and decrement operators|Decrement]]: <code>--<span style="color:gray;">x</span></code>, <code><span style="color:gray;">x</span>--</code> *[[Reference (computer science)|Address]]: <code>&<span style="color:gray;">x</span></code> *[[Indirection]]: <code>*<span style="color:gray;">x</span></code> *Positive: <code>+<span style="color:gray;">x</span></code> *Negative: <code>-<span style="color:gray;">x</span></code> *[[Ones' complement]]: <code>~<span style="color:gray;">x</span></code> *[[Negation|Logical negation]]: <code>!<span style="color:gray;">x</span></code> *[[Sizeof]]: <code>sizeof <span style="color:gray;">x</span>, sizeof(<span style="color:gray;">type-name</span>)</code> *[[Type conversion|Cast]]: <code>(''type-name'') ''<span style="color:gray;">cast-expression</span>''</code> ====Unix shell (Bash)==== In the [[Unix shell]] ([[Bash (Unix shell)|Bash]]/[[Bourne shell|Bourne Shell]]), e.g., the following operators are unary:<ref name="unixbash">{{cite web |title=Shell Arithmetic (Bash Reference Manual) |url=https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html |website=www.gnu.org |publisher=GNU Operating System |access-date=24 April 2024}}</ref><ref name="unarybash">{{cite web |last1=Miran |first1=Mohammad Shah |title=Unary Operators in Bash |url=https://linuxsimply.com/bash-scripting-tutorial/operator/unary-operators/ |website=LinuxSimply |access-date=24 April 2024 |date=26 October 2023}}</ref> *Pre and Post-Increment: <code>++<span style="color:gray;">$x</span></code>, <code><span style="color:gray;">$x</span>++</code> *Pre and Post-Decrement: <code>--<span style="color:gray;">$x</span></code>, <code><span style="color:gray;">$x</span>--</code> *Positive: <code>+<span style="color:gray;">$x</span></code> *Negative: <code>-<span style="color:gray;">$x</span></code> *Logical negation: <code>!<span style="color:gray;">$x</span></code> * Simple expansion: <code>$<span style="color:gray;">x</span></code> * Complex expansion: <code>${#<span style="color:gray;">x</span>}</code> ====PowerShell==== In the [[PowerShell]], the following operators are unary:<ref name="powershell">{{cite web |title=Expressions - PowerShell |url=https://learn.microsoft.com/en-us/powershell/scripting/lang-spec/chapter-07 |website=learn.microsoft.com |publisher=Microsoft |access-date=23 April 2024 |language=en-us |date=3 September 2021}}</ref> *Increment: <code>++<span style="color:gray;">$x</span></code>, <code><span style="color:gray;">$x</span>++</code> *Decrement: <code>--<span style="color:gray;">$x</span></code>, <code><span style="color:gray;">$x</span>--</code> *Positive: <code>+<span style="color:gray;">$x</span></code> *Negative: <code>-<span style="color:gray;">$x</span></code> *Logical negation: <code>!<span style="color:gray;">$x</span></code> *[[Execution (computing)|Invoke]] in current [[Scope (programming)|scope]]: <code>.<span style="color:gray;">$x</span></code> *Invoke in new scope: <code>&<span style="color:gray;">$x</span></code> *Cast: <code>[''type-name''] ''<span style="color:gray;">cast-expression</span>''</code> *Cast: <code>+<span style="color:gray;">$x</span></code> *Array: <code>,<span style="color:gray;">$array</span></code>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)