# Operators in Carbon Language

We have a number of operators in Carbon language like – Arithmetic, Bitwise, BitShift, Comparison, Conversion, Logical, Indexing, function call, pointer, move etc.

## Arithmetic

These arithmetic operators are supported –

• `-` : Negate ( -5 )
• `+` : Addition ( 2 + 3 )
• `-` : Subtraction ( 3 – 2 )
• `*` : Multiplication ( 2 * 3 )
• `/` : Division ( 3 / 2 )
• `%` : Modulo ( 3 % 2 )

Both signed and unsigned integers support all the arithmetic operations. Although division by zero is undefined.

Floating point numbers support all arithmetic operations except modulo.

Code Example –

``````var a: i32 = 5;
var b: i32 = 3;

// -5
var negation: i32 = -a;
// 8
var sum: i32 = a + b;
// 2
var difference: i32 = a - b;
// 15
var product: i32 = a * b;
// 1
var quotient: i32 = a / b;
// 2
var remainder: i32 = a % b;``````

## Bitwise & Bitshift

Supported bitwise & bitshift operators are –

• `^` : Complement ( ^5 )
• `&` : Bitwise AND ( 5 & 6 )
• `|` : Bitwise OR ( 5 | 6 )
• `^` : Bitwise XOR ( 5 ^ 6 )
• `<<` : Left Shift ( 5 << 2 )
• `>>` : Right Shift ( 5 >> 2 )

Code Example –

``````var a: u8 = 5;
var b: u8 = 3;
var c: i8 = -5;

// 250
var complement: u8 = ^a;
// 1
var bitwise_and: u8 = a & b;
// 7
var bitwise_or: u8 = a | b;
// 6
var bitwise_xor: u8 = a ^ b;
// 40
var left_shift: u8 = a << b;
// 2
var logical_right_shift: u8 = a >> 1;
// -3
var arithmetic_right_shift: i8 = c >> 1;``````

## Comparison

Here are the supported comparison operators –

• `==` : Equal ( 5 == 5 )
• `!=` : Not Equal ( 5 != 6 )
• `<` : Less Than ( 5 < 6 )
• `>` : Greater Than ( 6 > 5 )
• `<=` : Less than equal to ( 5 <= 6 )
• `>=` : Greater than equal to ( 5 >= 6 )

Code Example –

``````var a: bool = 5 == 5;
var b: bool = 5 != 6;
var c: bool = 5 < 6;
var d: bool = 6 > 5;
var e: bool = 5 <= 6;
var f: bool = 5 >= 6;``````

## Conversion

A conversion operator is used to convert one supporting data type to another. It is represented by `as`.

Code Example –

``````var n: i32 = 56;
var f: f32 = n as f32;``````

## Logical

Logical operators are used to build final condition from two expressions like `(5 > 6) or (5 < 3)`. These logical operators are supported in Carbon –

• `and` – Returns true when both expressions are true.
• `or` – Returns true if at least one of the expressions is true.
• `not` – To negate an expression.

Code Example –

``````var a: bool = (5 > 6) and (5 < 3)
// a = False and False = False

var b: bool = (5 < 6) or (5 < 3)
// b = True or false = True

var c: bool = not a;
// c = not False = True``````

## Indexing

Indexing is used to access values from array.

Code Example –

``````var a: [i32;] = (1, 2, 3);

var b: i32 = a[1]; // b = 2``````

## Function Call

Function call is an operator too.

Code Example –

``````fn Add(a: i64, b: i64) -> i64 {
return a + b;
}

var sum: i64 = Add(2, 3);  // sum = 5``````

## Move

Carbon will allow types to define if and how they are moved. This can happen when returning a value from a function or by using the move operator `~x`. This leaves `x` in an unformed state and returns its old value.

## Conclusion

In this article we saw all the operators which are supported by Carbon Language. In the next post we will learn about conditions, loops and functions.

Get only the best articles delivered to your mail

## Switch (Match) Conditional in Carbon Language

Table of Contents Hide IntroductionCode ExampleConclusion In Carbon language we don’t have switch statement. Else we have match. match is a control flow similar to switch of C and C++ and mirrors similar…

## Struct in Carbon Language – Composite Types

Table of Contents Hide Properties of StructWhere Struct type can be used?Code ExampleConclusion Struct in carbon language is a composite type whose members are accessed through name and not by…

## Ternary Operator (If expressions) in Carbon

Ternary operators are generally used to return a value based on conditions in a single statement. This prevents unnecessary variable declarations and memory consumption. In Carbon we have if expressions…

## Tuples in Carbon Language – Composite Types

Table of Contents Hide Code ExamplesHow to access a tuple value?Conclusion A tuple in carbon language is a fixed-size collection of values that can have different types, where each value…

## Functions in Carbon Language

Table of Contents Hide IntroductionCode Example –Conclusion Functions in carbon language has the same role as in C, Php or any other functional language. It is used to separate a…

## Loops in Carbon Language – while, for, break, continue, return

Table of Contents Hide while Loopfor LoopbreakcontinuereturnConclusion We can use for and while loops in carbon language. Within a loop the break and continue statements can be used for flow…