**Overview**

Carbon has almost same operators with C++. Therefore, if you are familiar with them, you can skip this part.

As always, the reader can find the source codes of part from this github link.

**Arithmetic operators**

Negation: -

Addition operator: +

Substraction operator: -

Multiplication operator: *

Division operator: /

Modulo operator: %

Let's take a quick look to these operators:

```
var myVariable: i32 = 3;
myVariable = -myVariable;
Print("Negation of 3 is: {0}", myVariable);
Print("Sum of 6 and 3 is: {0}",6 + 3);
Print("Difference of 6 and 3 is: {0}",6 - 3);
Print("Multiplication of 6 and 3 is: {0}",6 * 3);
Print("6 divided by 3 is: {0}",6/3);
Print("Mod3 of 5 is: {0}",5 % 3);
```

**Conversion in Carbon**

To change type of a variable, we use conversion. Carbon supports conversion operations with as keyword.

```
var myConvertable: i32 = 3;
var convertedVersion: f32 = myConvertable as f32;
```

**Conditional operators**

Logical operators are pretty same. Since we did not learn conditional statements yet, they are also useless for us, now :)

Equals to ==

Not equals to !=

Greater than >

Greater or equals to >=

Less than <

Less than or equals to <=

Here are few examples for such operators:

```
var s: bool = (22 == 22);
var a: bool = (8 > 2022);
var d: bool = (8 >= 2022);
var i: bool = (8 < 2022);
```

**Logical operators**

Logical operators are same with python:

and keyword for and operation (instead of &&)

or keyword for or operation (instead of ||)

not keyword for logical complement (instead of !)

```
var g: bool = s and a;
var l: bool = d or i;
var b: bool = not i;
```

**Bitwise Operators**

^ for complement (before the value)

& for bitwise AND (between the values)

| for bitwise OR (between the values)

<< for left shift (between the values)

for right shift (between the values)

^ for XOR (between the values)

Let's look at some examples to understand better:

We will use 32 bit integers and to see the effects better, their values as bits will be given.

```
var first: i32 = 7; // 00000000 00000000 00000000 00000111
var second: i32 = 2; // 00000000 00000000 00000000 00000010
```

Let's take the complement of the first variable by hand. To take the complement, we basically change 0's to 1 and 1's to 0's:

11111111 11111111 11111111 11111000

Since it is 2 based signed integer, to find its value, we should take 1's complement and add 1 to it. However, we already did the 1's complement, therefore original value is 1's complement. Adding 1 to it makes it 8. Then the result should be -8.

Let's check it with our code:

`Print("Complement of first: {0}", ^first); // Output: -8`

And operation is pretty easy. We look at the 1s that are in the same location. Then it should be:

00000000 00000000 00000000 00000010

Let's check it with our code:

`Print("first and second: {0}", first & second); // Output: 2`

And keep going with other operators:

```
Print("first or second: {0}", first | second); // Output: 7
Print("first xor second: {0}", first ^ second); // Output: 5
Print("first left shift by 1: {0}", first << 1); // Output: 14
Print("first right shift by 1: {0}", first >> 1); // Output: 3
```

**Conclusion**

Even though these are not all operators in Carbon, one can see that they are similar to other programming languages. However, even though Carbon is a successor of C++, there are some differences.

## Top comments (0)