1.
Introduction
2.
Operators in Carbon
2.1.
Arithmetic Operators
2.2.
Bitwise Operators
2.3.
Comparison Operators
2.4.
Conversion Operator
2.5.
Logical Operators
2.6.
Conditionals
3.
Operator Precedence
4.
4.1.
What is Carbon?
4.2.
How are variables declared in Carbon?
4.3.
What are the Operators in Carbon?
4.4.
How can we convert the data type of variables in Carbon?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Operators and Precedence in Carbon

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

## 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Operator Precedence

Operator Precedence refers to the priority of orders. If an operator is placed higher in the list, it will be applied first. It is used to evaluate mathematical expressions containing more than one operator.

The precedence of the operators in Carbon is as follows:

### What is Carbon?

Carbon is a successor of the C++ language launched by the developers of Google. It is in the experimental stage and used to solve the difficulties of C++ and has advanced features like memory safety, generics, etc.

### How are variables declared in Carbon?

Variables are declared with the var keyword, and is used after the variable names. For example, var x: i32 = 10. i32 specifies the integer type in this example.

### What are the Operators in Carbon?

Operators are the symbols used to represent an expression. They are used to perform operations on the values or variables. Different types of operators in Carbon are Arithmetic, Logical, Bitwise, Comparison, Logical, Conversion, and conditional.

### How can we convert the data type of variables in Carbon?

We can explicitly cast the data types of variables in Carbon using the Conversion operator ( as ). Syntax of as expression is var y: f32 = x as f32.

## Conclusion

This article was about Operators and Precedence in Carbon language. We have learned about various operators like Arithmetic operators, logical operators, bitwise operators, comparison operators, etc., with examples. We have also discussed the precedence of the operators in carbon.

You can check out our other articles if you want to dive deep into other concepts related to Carbon language -

Check out this problem - XOR Queries On Tree

You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questionsand the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow.

Happy Coding !!

Live masterclass