Open In App

Difference between Unary and Binary Operators

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
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:

Aspect Unary Operators Binary Operators
Number of Operands Operates on one operand Operates on two operands
Example -x, ++i, !flag x + y, a * b, value == 10
Operations Typically perform operations on a single value Perform operations involving two values
Syntax and Usage May appear before or after the operand Appears between the operands
Purpose Used for operations like negation, increment, etc. Used for arithmetic operations, comparisons, etc.
Associativity Some have prefix and postfix forms (++i, i++) Usually left-associative (a + b + c)

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads