## What are Operators in Programming?

An operator is a symbol that indicates an operation to be performed. We are familiar with operators in mathematics; operators used in computer programming are—in many ways—similar to mathematical operators.

**Operators**are used in**expressions**.- An
**expression**is an instruction that combines one or more**operands**, zero or more**operators**, and zero or more pairs of parentheses. **Operands**are the terms on which an operator**performs an operation**and gives a result.- So, the expression “a+b” can be broken down into the following three components:
- a, which is the left operand
- the plus symbol (+), which is the operator
- b, which is the right operand

An operator in computer programming is a symbol that has a defined function. An operator tells the compiler or interpreter to perform specific mathematical, logical, or other operations—or manipulations—on the operands to arrive at a result.

## How many are the Basic Operators?

Before learning about the basic operators used across computer programming languages, let’s first look at the types of operators and how they are categorized. Operator types and categories are depicted in the diagram below:

The classification of operators into unary, binary and ternary is based on the number of operands an operator requires.

Unary operators operate on a single operand. The increment operator (++) and the decrement operator (--) are examples of unary operators. The table below describes these operators:

### Unary operators

**Unary operators** operate on a single operand. The *increment operator* (++) and the *decrement operator* (--) are examples of unary operators. The table below describes these operators:

### Binary operators

**Binary operators** operate on two operands. The *addition operator* (+) and the *division operator* (/) are examples of binary operators.

The following are the five types of binary operators used in computer programming:

- Arithmetic
- Logical
- Bitwise
- Relational
- Assignment

**Arithmetic Operators** perform arithmetic operations. The table below describes the arithmetic operators:

Operator | Definition | Syntax | Operation | Example |

+ | Addition | x+y | Adds the left operand and the right operand | x = 5, y = 2 after c = x+y c = 7 |

- | Subtraction | x-y | Subtracts the right operand from the left operand | x = 5, y = 2 after c = x-y c = 3 |

* | Multiplication | x*y | Multiplies the left operand and the right operand | x = 5, y = 2 after c = x*y c = 10 |

/ | Division | x/y | Divides the left operand by the right operand | x = 6, y = 2 after c = x/y c = 3 |

% | Modulus | x%y | Divides the left operand by the right operand and returns the remainder | x = 5, y = 2 after c = x%y c = 1 |

**Logical operators** combine two or more conditions and return a Boolean value as the result.

**Note:** An exception among the logical operators is that logical NOT is a *unary* operator.

The table below describes the logical operators:

Operator | Definition | Syntax | Operation | Example |

&& | logical AND | x && y | Returns true if both x and y are true, else returns false | x = true, y = true after c = x && y c = true |

|| | logical OR | x || y | Returns true if either x or y is true, else returns false | x = true, y = false after c = x && y c = true |

! | logical NOT | !x | Returns true if x is false, else returns false | x = true after c = !x c = false |

**Relational operators** compare values and return a Boolean value as the result. The table below describes the relational operators:

Operator | Definition | Syntax | Operation | Example |

== | equal | x == y | Returns true if the values of the operands are equal; returns false if the values of the operands are not equal | x = 5, y = 5 after c = (x == y) c = true |

!= | not equal | x != y | Returns true if the values of the operands are not equal; returns false if the values of the operands are equal | x = 5, y = 2 after c = (x != y) c = true |

> | greater than | x > y | Returns true if the value of the left operand is greater than the value of the right operand, else returns false | x = 5, y = 2 after c = (x > y) c = true |

< | less than | x < y | Returns true if the value of the left operand is less than the value of the right operand, else returns false | x = 5, y = 2 after c = (x < y) c = false |

>= | greater than or equal | x >= y | Returns true if the value of the left operand is greater than or equal to the value of the right operand, else returns false | x = 5, y = 5 after c = (x >= y) c = true |

<= | less than or equal | x <= y | Returns true if the value of the left operand is less than or equal to the value of the right operand, else returns false | x = 5, y = 2 after c = (x <= y) c = false |

**Bitwise operators** use the binary representations of the operands and perform the indicated operation on each corresponding bit.

**Note:** An exception among the bitwise operators is that bitwise NOT is a *unary* operator.

The table below describes the bitwise operators:

Operator | Definition | Syntax | Operation | Example |

& | bitwise AND | x & y | Uses the operands’ binary values and performs the AND operation on the corresponding bits | x = 8, y = 2 after c = x & y c = 1000 & 0010 c = 0000 = 0 |

| | bitwise OR | x | y | Uses the operands’ binary values and performs the OR operation on the corresponding bits | x = 12, y = 2 after c = x | y c = 1100 | 0010 c = 1110 = 14 |

^ | bitwise XOR | x ^ y | Uses the operands’ binary values and performs the XOR operation on the corresponding bits | x = 8, y = 10 after c = x ^ y c = 1000 | 1010 c = 0010 = 2 |

<< | left shift | x << y | Moves the bit values of the left operand to the left by the number of bits the right operand specifies; this is the same as multiplying x by 2^{y} | x = 8, y = 2 after c = x << y c = 1000 << 0010 c = 100000 = 32 |

>> | right shift | x >> y | Moves the bit values of the left operand to the right by the number of bits the right operand specifies; this is the same as dividing x by 2^{y} | x = 8, y = 2 after c = x >> y c = 1000 >> 0010 c = 0010 = 2 |

~ | bitwise NOT | ~x | Flips each bit of the operand | x = 7 after c = ~x c = ~0111 c = 1000 = 8 |

**Assignment operators** assign the result of the specified operation to the left operand; no additional variable is used to store the result. The simple assignment operator (=) assigns the value of the right operand to the left operand. The table below describes the assignment operators:

Operator | Definition | Syntax | Operation | Example |

= | assignment | x = y | Assigns the value of the right operand to the left operand | x = 3, y = 2 after x = y x = 2 |

+= | addition and assignment | x += y | Adds the right operand to the left operand and assigns the result to the left operand | x = 5, y = 2 after x += y x = x+y x = 7 |

-= | subtraction and assignment | x -= y | Subtracts the right operand from the left operand and assigns the result to the left operand | x = 5, y = 2 after x -= y x = x-y x = 3 |

*= | multiplication and assignment | x *= y | Multiplies the right operand by the left operand and assigns the result to the left operand | x = 5, y = 2 after x *= y x = x*y x = 10 |

/= | division and assignment | x /= y | Divides the left operand by the right operand and assigns the result to the left operand | x = 6, y = 2 after x /= y x = x/y x = 3 |

%= | modulus and assignment | x %= y | Takes the modulus of the two operands and assigns the result to the left operand | x = 5, y = 2 after x %= y x = x%y x = 1 |

**Ternary operators** operate on three operands. The conditional operator (?) is an example of a ternary operator. It uses the syntax (condition? option1: option2).

The conditional operator is an alternative to the if-else statement. The operator first evaluates the condition. If the condition is true, option1 will be the resultant value; if the condition is false, option2 will be the resultant value.

The following is an example of how the conditional operator can be used:

Suppose we need to find the larger number from among a and b.

Consider the expression num = (a > b) ? a : b

Here, *num* is the variable that will store the larger number from among *a* and *b*.

The expression can be evaluated thus: If *a* is greater than *b*, then *num* = *a* else *num* = *b*.

## Operator Precedence

In computer programming, knowledge of operator precedence and associative rules are essential. Operator precedence is similar to the PEMDAS rule in mathematics.

When two operators with different precedence are adjacent to each other in an expression, which operator should be evaluated first is based on the **operator precedence** rule.

When two operators with the same precedence are adjacent to each other in an expression, which operator should be evaluated first is based on **associative rules**—which tell the compiler whether it should evaluate from left to right (**LR**) or from right to left (**RL**).

The following table shows operator precedence and lists associative rules. The higher in the table an operator is, the higher its precedence.

Definition | Operator(s) | Associativity |

Post-increment, post-decrement | ++, -- | LR |

Pre-increment, pre-decrement | ++, -- | RL |

Logical NOT, Bitwise NOT | ! | RL |

Multiplication, Division, Modulus | *, /, % | LR |

Addition, Subtraction | +, - | LR |

Shift | >>, << | LR |

Relational less than, less than or equal to | <, <= | LR |

Relational greater than, greater than or equal to | >, >= | LR |

Equality | ==, != | LR |

Bitwise AND | & | LR |

Bitwise XOR | ^ | LR |

Bitwise OR | | | LR |

Logical AND | && | LR |

Logical OR | || | LR |

Conditional | ?: | RL |

Assignment | =, *=, /=, %=, +=, -= | RL |

## Context and Applications

This topic is significant for learning basics of programming and it is also important in professional exams for undergraduate courses especially for:

- Bachelor of Technology in Computer science
- Bachelor of Technology in Computer engineering
- Bachelor of Technology in Information Technology
- Bachelor of Engineering in Computer science
- Bachelor of Engineering in Computer engineering
- Bachelor of Engineering in Information Technology
- Bachelor of Computer Applications (BCA)

### Want more help with your computer science homework?

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

### Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

## Basic Operators Homework Questions from Fellow Students

Browse our recently answered Basic Operators homework questions.

### Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.