Kotlin contains several built-in operators, which are functions that employ symbolic name representation. They are used to perform operations on operands. In this article on Kotlin operators, we will be learning all about Kotlin operators in detail, with the help of examples.

## What Are Kotlin Operators?

An operator is a unique symbol that is used to perform a specific operation on its operand. An operator is a symbol that instructs the compiler or interpreter to perform a specified mathematical, relational, or logical operation and returns the result.

In Kotlin, we have many built-in operators for performing various operations. Although Kotlin operators can operate with a wide range of variables and constants, some of them are limited to working with specific data types.

Now let us understand the Types of operators in Kotlin.

## Types of Operators in Kotlin

There are various types of operators in Kotlin.

- Arithmetic Operators

- Assignment Operators

- Comparison Operators

- Logical Operators

- Unary Operators

- Bitwise Operators

Now let's learn about these operators in detail.

## Arithmetic Operators

Arithmetic Operators are used to perform arithmetic or mathematical operations like addition, subtraction, multiplication, division, modulus, etc.

+ To add two values, use the addition operator.

- Subtraction operator is used to subtract one value from another.

* Two values, a and b, are multiplied using the multiplication operator.

/ To divide one value by another, the division operator is used.

% The remainder of the division is returned by the modulus operator.

Let's have a look at the example in which we are performing all the arithmetic operations.

The results of the above example are shown below.

## Assignment Operators

We can use assignment operators to assign a variable value or the result of an arithmetical expression to a variable.

+= Add a to b and assign to a

-= Subtract b from a and assign to a

*= Multiply a and b and assign to a

/= Divide a by b and assign to a

%= Divide a by b and assign the remainder to a.

Let us have a look at the example of the Assignment operator.

In this example n1+=n2 means n1=n1+n2 which is 75(60+15). Similarly, n1-=n2 means n1=n1-n2, which is 45(60-15), and this goes for all other expressions as well.

Now let us understand Comparison operators.

## Comparison Operators

A comparison between two operands is evaluated using comparison operators. A Boolean value, which can only be true or false, is the outcome of a comparison process. Comparison operators are also known as relational operators.

a>b Checks whether a is greater than b.

a<b Checks whether a is smaller than b or not.

a>=b Checks whether a is greater than or equal to b or not.

a<=b Checks whether a is smaller than or equal to b or not.

a==b Checks whether a is equal to b or not.

a!=b Checks whether a is not equal to b or not

Let's have a look at the example of the Comparison operator.

In this example, we are comparing n1 and n2 and checking which one is greater among them.

Whichever is greater will be printed in the output.

## Logical Operators

The logical (AND, OR, NOT) operators are used to combine expressions with conditional statements, yielding true or false results.

&& (AND) If every expression is true, it returns true.

|| (OR) It returns true if any expressions are true.

! (NOT) It returns the negation of the expression.

Let us have a look at an example.

In this example, we have used the AND operator in between the comparisons of n1, n2, and n3. The AND operator will return true only if n1 is greater than n2 and n3 both as per expression, i.e., if both the expressions evaluate to be true, otherwise it will return false.

In case of the OR operator, if any of the expressions evaluates to be true, then the OR operator will return true.

The NOT operator is returning the negation of the value p

The results of the above example are shown below.

## Unary Operators

Unary operators are those operators that work with only one operand.

In Kotlin, there are 5 types of unary operators.

- Unary Plus Operator (+)
- Unary Minus Operator (-)
- Increment Operator (++)
- Decrement Operator (--)

Unary Plus Operator(+): A number without a sign is a positive number, the unary plus operator returns the positive value.

Unary Minus Operator(-): This operator returns the negative value.

Increment Operator(++): Using this operator, the value is raised by 1; this increment operator can be pre-increment or post-increment.

Decrement Operator(--): This operator decreases the value by 1; this operator can also be pre-decrement or post-decrement.

Let us have a look at an example where we are using these operators.

The results of the above example are shown below.

## Bitwise Operators

Kotlin doesn't really have a bitwise operator, but kotlin has some functions that help in performing those tasks.

- shl - Signed shift left: The bits are shifted to the left, the far left bit is discarded, and the rightmost bit is set to 0.
- shr - Signed shift right: Comparing pairs of bits, the bitwise (inclusive) OR operator returns 1 if either or both of the bits are 1, else 0.
- ushr - Unsigned shift right operator shifts zero into the leftmost position.
- and - Bitwise and: The bitwise AND operator compares pairs of bits and returns 1 if both are 1, otherwise 0.
- or - Bitwise or: Comparing pairs of bits, the bitwise (inclusive) OR operator returns 1 if either or both of the bits are 1, else 0.
- xor - Bitwise xor: The bitwise exclusive OR (XOR) function compares pairs of bits and returns 1 if the bits are complementary, 0 otherwise.
- inv - Bitwise inversion: The bitwise inv() function inverts the bit pattern and makes every 0 to 1, and every 1 to 0.

Now let us have a look at an example.

Here in this example, a.and(b), and function will compare the bits of a and b, i.e., 12 and 6.

In and function, if both bits are 1 then it evaluates to be 1; otherwise, if either of the bits is zero, then it is evaluated to zero.

Similarly, or function will compare the bits of both a and b. In or function, if either of the bits is 1 it will give 1; otherwise, it will give zero.

In case of a.xor(b), if both the bits are same, it will give 0, and if both are different, then it will give 1.

The a.inv() function will invert every 0 to 1 and 1 to 0.

The shl function adds zero bits while shifting some specified bits to the left in a bit pattern.

Similarly, The shr function moves a bit pattern rightward by a specified amount of bits.

The ushr function is a right shift operator that doesn't maintain the sign of the original number and fills the new position with zero.

The results of the above example are shown below.

If you're eager to gain the skills required to work in a challenging, rewarding, and dynamic IT role - we've got your back! Discover the endless opportunities through this innovative Post Graduate Program in Full Stack Web Development course designed by our partners at Caltech CTME. Enroll today!

## Conclusion

In this tutorial on Kotlin operators, you understood types of operators, and various topics including Arithmetic operators, Assignment operators and Comparison operators. You also learned about Logical operator, Unary operator, and Bitwise operator in detail with the help of examples.

If you are looking to build a software development career, you can check the Post Graduate Program in Full Stack Web Development by Simplilearn. It might be the best option for assisting you in advancing your profession.

Do you have any questions about this Kotlin Operators tutorial? If you do, you can include them in the comments section below. Your questions will be addressed by us at the earliest.