**Introduction**

Hey Ninjas! We will be talking about the Operators and Precedence in Carbon. You all must have heard about Carbon in recent times. It is gaining popularity as the successor of the C++ language. It solves the problem of C++ and has advanced features.

This article will cover the various operators in carbon like Arithmetic, Bitwise, Logical, Conditional, etc. We will also discuss the precedence of various operators. So stay motivated, and letâ€™s begin by learning what operators are.

**Operators in Carbon**

Operators are the tools used to perform operations on the variables or the values. They are used to perform specific operations like arithmetical, logical, conditional, etc. For example, addition can be performed using the + operator.

Now letâ€™s discuss the operators available in Carbon.

**Arithmetic Operators**

Arithmetic operators like (+, -, *, /, %, ++, - ) are used to perform arithmetic ( mathematical ) operations on the operands. Here are the arithmetic operators with their functions:

Now, letâ€™s see an example of using these operators.

```
package ExplorerTest api;
fn Main() -> i32 {
var a: i32 = 1;
var b: i32 = 2;
Print("Negation of a is {0}", -a);
Print("Sum is {0}", a+b);
Print("Difference is {0}", a-b);
Print("Product is {0}", a*b);
Print("a divided by b is {0}", a/b);
Print("a modulo b is {0}", a%b);
return 0;
}
```

Output for the same is shown below:

**Bitwise Operators**

The bitwise operators are used to perform the mathematical operations at the bit level. Following are the bitwise operators in Carbon:

Letâ€™s look at the implementation of these operators with an example:

```
package ExplorerTest api;
fn Main() -> i32 {
var a: i32 = 1;
var b: i32 = 2;
Print("AND of a,b is {0}", a&b);
Print("OR of a,b is {0}", a|b);
Print("XOR of a,b is {0}", a^b);
Print("a left shift b is {0}", a<<b);
Print("a right shift b is {0}", a>>b);
return 0;
}
```

For the above implementation, the output is shown below:

**Comparison Operators**

The comparison operators compare two values or variables. The following table shows comparison operators with their syntax and function:

The implementation of these operators is shown below:

```
package ExplorerTest api;
fn Main() -> i32 {
var a: i32 = 1;
var b: i32 = 2;
if(a==b){
Print("a is equal to b");
}
else if(a>b){
Print("a is greater than b");
}
else{
Print("a is less than b");
}
return 0;
}
```

Since a is 1 and b is 2, a is less than b.

**Conversion Operator**

The conversion operators in carbon are used to change the data type of the variables. We can cast any expression from one type to another using an **as **expression:

```
var a: i32 = 10;
var b: f32 = a as f32;
```

This will convert the integer data type to float.

**Logical Operators**

The logical operators include **and, or, **& **not. **These are used to combine two or more conditions to evaluate the results.

Letâ€™s look at an example.

```
package ExplorerTest api;
fn Main() -> i32 {
var a: bool = true;
var b: bool = true;
// This will convert true to false
b = not b;
if(a and b){
Print("Both a and b are true");
}
else if(a or b){
Print("Either a or b is true");
}
else{
Print("Both are false");
}
return 0;
}
```

In this example, a and b are assigned as true. With the help of the **not **operator, b is converted to false. Now since a is true and b is false, **or **will return true.

**Conditionals**

An **if** expression is defined as ( **If **condition1 **then **value1 **else **value2 ). This works the same as a ternary operator in other languages. The following expression will return value1 if the condition is met; else, it will return value2.

```
package ExplorerTest api;
fn Main() -> i32 {
var a: i32 = if (1==2) then 10 else 20;
Print("{0}", a);
return 0;
}
```

Since 1 is not equal 2, the condition is false; therefore, a will be assigned 20.

These were all the operators used in Carbon; now, letâ€™s move on to understand the precedence of these operators.