Operators in Carbon Language

Total
0
Shares
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

Pointer

Another operator is pointer. Learn more about pointer here – Pointers in Carbon

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.

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like
functions in carbon language

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…
View Post