# Scala Operators

In this tutorial, we will learn about the different types of operators in Scala. By Shivang Yadav Last updated : April 02, 2023

## What are operators in Scala?

An operator is a symbol that represents an operation that is to be performed in the program. Operators tell the compiler to perform a specific operation, each operator is associated with one and has a unique identification code. Operators play an important role in programming and they are used to make an expression that executes to perform a task. It defines that operation between operands, based on the operator their can be one, two or three operands.

## Types of Scala Operators

Scala as a huge range of operator that can be used while programming in Scala. They are:

1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Bitwise operators
5. Assignment operators

### 1) Scala Arithmetic Operators

For performing arithmetic operations in Scala, arithmetic operators are defined. The following arithmetic operators are defined in Scala,

Operator Description
12 + 45 = 57
Subtraction Operator (-) Subtracts second operator from first
23 - 4 = 19
Multiplication Operator (*) Multiplies the two operands
25 *6 = 150
Division Operator (/) Divides the first operator by second
124/4 = 31
Modulus Operator (%) Gives the remainder when the first operator is divided by the second
123 % 5 = 3

### 2) Scala Relational Operators

A relational operator is used to define the comparison between the two operators. It is also known as a comparison operator. Following are relational Operators in Scala,

Operator Description
Equal to (==) Compares if the two operands are equal, if yes then outputs TRUE else FALSE
12 == 34 → FALSE
23 == (46/2) → TRUE
Not Equal to (!=) Compares if the two operands are not equal, if yes then outputs TRUE else FALSE
12 != 34 → TRUE
23 != (46/2) → FALSE
Greater than (>) Checks if operand 1 is greater than operand 2. If yes outputs true otherwise false
34 > 2 → TRUE
34 >334 → FALSE
Greater than equal to (>=) Checks if operand 1 is greater than or equal to operand 2. If yes outputs true otherwise false
34 >= 2 → TRUE
34 >=334 → FALSE
34 >= 34 → TRUE
Less than (<) Checks if operand 1 is smaller than operand 2. If yes outputs true otherwise false
34 < 2 → FALSE
34 < 334 → TRUE
Less than equal to (<=) Checks if operand 1 is smaller than or equal to operand 2. If yes outputs true otherwise false
34 <= 2 → FALSE
34 <= 334 → TRUE
34 <= 34 → TRUE

### 3) Scala Logical Operators

A logical Operator is used to combine the logical output (true / False ) or reverse to the logical output of a condition or operand. Scala defines the following logical operators,

Operator Description
And (&& or and) It outputs TRUE, if the logical value of both operands is TRUE, else outputs FALSE.
OR (|| or or) It outputs FALSE, if the logical value of both operands is FALSE, else outputs TRUE.
NOT (! or not) It reverses the output of the operand.

The results of and , or and, not operator based on different values of operand:

 A B A && B A || B !A True True True True False True False False True False False True False True True False False False False True

### 4) Scala Bitwise Operators

It works on the bits of the operands. It operates on every bit of the operands to give output. The following are valid bitwise operators in Scala,

Operator Description
Bitwise AND (&) It takes every bit of both operands and performs AND operation to it.
2 & 6 = 2 -> 010 & 110 = 010
Bitwise OR (|) It takes every bit of both operands and performs OR operation to it.
2 | 6 = 6 -> 0010 | 0110 = 0110
Bitwise XOR (^) It takes every bit of both operands and performs XOR operation to it.
XOR operation gives 1 output when both operands are different.
2 ^ 6 = 4 -> 0010 ^ 0110 = 0100
Bitwise Left Shift (<<) It shifts left the bits of operand 1 by n bits; n is specified by operand 2.
12 << 3 = 96 -> 0000 1100 << 3 = 0110 0000
Bitwise Right Shift (>>) It shifts right the bits of operand 1 by n bits; n is specified by operand 2.
12 >> 3 = 129 -> 0000 1100 >> 3 = 1000 0001
Bitwise Right Shift zero fill (>>>) It shifts the bits of operand 1 by n bits; n is specified by operand 2. And fills the shifted bit by 0.
12 >>> 3 = 1 ->0000 1100 >>> 3 000 0001
Bitwise Complement (~) It takes every bit of the operand and reverses its bits.
~12 = 243 -> ~ (0000 1100) = 1111 0011

### 5) Scala Assignment Operators

The assignment operator is used to assign value to a variable. The variable is at the left of the operator and value is at the right of the operator. The data type needs to be the same for both sides to avoid errors in the program.

The following are valid assignment operators in Scala,

Operator Description
Simple Assignment (=) It simply assigns the value at left to the variable at right.
a = 2432;
Add and assign (+=) It adds the left value to the right value.
c += 32 -> c = c + 32
Subtract and assign (-=) It subtracts the left value from the right value.
c -= 32 -> c = c - 32
Multiply and assign (*=) It multiplies the left value with the right value.
c *= 32 -> c = c * 32
Divide and assign (/=) It divides the left value from the right value.
c /= 32 -> c = c / 32
Remainder and assign (%=) It finds the modulus of to the right value divide by left value.
c %= 32 -> c = c % 32
Left Shift and assign (>>=) It shifts the bits of left operands by n , n defined by right operator and assigns this value to left operator.
i >>= 6 -> i = i >> 6
Right Shift and assign (<<=) It shifts the bits of right operands by n , n defined by right operator and assigns this value to left operator.
i <<= 6 -> i = i << 6
Bitwise and assignment (&=) It performs the bitwise and operation of left operand and right operand and assigns this value to left operator.
i &= 6 -> i = i & 6
Bitwise exclusive OR assignment (^=) It performs the bitwise exclusive OR operation of left operand and right operand and assigns this value to left operator.
i ^= 6 -> i = i & 6

All the operators are applied in Scala as it is and you can use them easily but to avoid error data type of operands should be the same.