Open In App

Difference between Unary and Binary Operators

Last Updated : 20 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Unary Operators and Binary operators are both fundamental concepts in computer science and programming languages, especially in the context of arithmetic and logical operations. Here's a breakdown of the differences between them:

Unary Operators:

Unary Operator is an operator that operates on a single operand, meaning it affects only one value or variable. Unary operators are commonly used in programming languages to perform various operations such as changing the sign of a value, incrementing or decrementing a value, or performing logical negation.

Examples of Unary Operators:

Here are the examples of Unary Operator in Programming:

  • Unary minus (-x)
  • Increment (++)
  • Decrement (--)
  • Logical NOT (!x)

Binary Operators:

Binary Operator is an operator that operates on two operands, meaning it affects two values or variables. Binary operators are commonly used in programming languages to perform various operations such as arithmetic operations, logical operations, and bitwise operations.

Examples of Binary Operators:

Here are the examples of Binary operator in Programming:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Assignment (=)
  • Logical AND (&&)
  • Logical OR (||)

Difference between Unary and Binary Operators:

AspectUnary OperatorsBinary Operators
Number of OperandsOperates on one operandOperates on two operands
Example-x, ++i, !flagx + y, a * b, value == 10
OperationsTypically perform operations on a single valuePerform operations involving two values
Syntax and UsageMay appear before or after the operandAppears between the operands
PurposeUsed for operations like negation, increment, etc.Used for arithmetic operations, comparisons, etc.
AssociativitySome have prefix and postfix forms (++i, i++)Usually left-associative (a + b + c)

Next Article
Article Tags :

Similar Reads