# Understanding F# Operators

In this article, You will learn how to effectively use **F# operators**, whether you are new to the language or a pro. We’ll be discussing the different operators that can be used in F# and their syntax.

The F# a programming language provides a wide variety of operators that can be used for operations such as **arithmetic**, **comparisons**, **logic**, and many others operations.

## F# Operators

F# operators are important because they provide a concise and expressive way to perform common operations on various types of data. Without operators, you would need to write complex functions to perform these operations, resulting in longer and less readable code.

F# operators are designed to work with the functional programming paradigm, which emphasizes immutable data and pure functions.

They are designed to work seamlessly with other F# features such as pattern matching, list comprehensions, and type inference. This allows you to write code that is concise, expressive, and easy to understand.

There are a number of operators in F# that are similar to operators in other programming languages, but F# has some particular features that make it especially well suited to working with operators compared to other languages.

A compiler uses an operator to tell it to perform a specific task in the form of a mathematical or logical operation.

There are many built-in operators in F#, and some of the most common types of operators are listed below:

- Arithmetic operators
- Comparison operators
- Boolean operators
- Bitwise operators

## F# Arithmetic Operators

There are several arithmetic operators that are available in the F# language as shown in the following table.

Let’s assume that variable **x** holds 5 and variable **y** holds 15, then the following will happen:

Operators | Overview | Examples |

+ | The addition of two operands is made | x + y will give 25 |

– | The subtraction of two operands is made | x – y will give -10 |

* | The multiplication of two operands is made | x * y will give 75 |

/ | The division of two operands is made | y / x will give 3 |

% | Prints the remainders of two variables | y % x will give 0 |

** | Exponentiation Operator: This operator can be used on operands that have been multiplied by another operand to get the result. | y**x will give 155 |

#### Example:

**Output:**

Sum is: 20Subtraction is: -10 Multiplication is: 75 Division is: 0 Modulus is: 5 Exponentiation is: 759375.0

## Comparison Operators

There are a number of comparison operators that are supported by the F# language and they are shown in the following table.

The term binary comparison refers to comparing two types of data, integrals and floating point values.

A value of type bool is returned by each of these operators.

Let’s assume that variable x holds 5 and variable y holds 15, then the following will happen:

Operators | Overview | Examples |

= | This function determines whether two operands are equal or not by comparing their values. The condition becomes true if the operands are equal. | (x == y) is not true. |

<> | The function checks to see if two operands have the same values or not. This condition will become true if no values are equal, otherwise it will become false. | (x == y) is true. |

> | A condition is true if the value of the left operand is greater than the value of the right operand. If not, then the condition is false. | (x > y) is not true. |

< | A condition is true if the value of the right operand is greater than the value of the left operand. If not, then the condition is false. | (x > y) is true. |

>= | A condition is true if the value of the left operand is greater than or equal to the value of the right operand. If not, then the condition is false. | (x >= y) is not true. |

<= | A condition is true if the value of the right operand is greater than and equal to the value of the left operand. If not, then the condition is false. | (x <= y) is true. |

#### Example:

**Output:**

x is not equal to y x is not equal to y x is less than y x is not greater than y x is less or equal to y x is a is less than y

## Boolean Operators

A table is provided below that lists all the Boolean operators that are supported by F#. Assuming variable x is true and variable y is false, then:

Operators | Overview | Examples |

&& | It is also called the Boolean AND operator. This condition is true if both operands are non-zero. | (x && y) is false. |

|| | It is also called the Boolean OR operator. This condition is true if any of the two operands are non-zero. | (x || y) is true. |

not | It is also called the Boolean NOT Operator. To reverse its operands’ logical state. By using the Logical NOT operator, a true condition becomes false and false condition becomes true. | not (x && y) is true. |

#### Example:

**Output:**

X && Y Condition is true X || Y Condition is true not (x && y) Condition is not true X && Y Condition is not true X || Y Condition is true not (x && y) Condition is true

## Bitwise Operators

Using bitwise operators, you can perform bit-by-bit operations.

This statement shows the truth tables for the bitwise (**AND**), the bitwise (**OR**), and the bitwise exclusive OR operators:

p | q | p &&& q | p ||| q | p ^^^ q |

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

1 | 0 | 0 | 1 | 1 |

Suppose that **x = 45**, and **y = 15**, and then in binary format they will be as follows:

A = 0010 1101 B = 0000 1111 A&&&B = 0000 1101 A|||B = 0010 1111 A^^^B = 0010 0010 ~~~A = 1101 0001

Below is a table that lists all the Bitwise operators that are supported by the F# language.

If we assume that variable x represents 45 and variable y represents 15, we can conclude:

Operators | Overview | Examples |

&&& | Using a binary AND operator, if the bit is present in both operands, it is copied to the result. | (x &&& y) will give 13, which is 0000 1101 |

||| | The function checks to see if two operands have the same values or not. This condition will become true if no values are equal, otherwise it will become false. | (x ||| y) will give 47, which is 0010 1111 |

^^^ | A condition is true if the value of the left operand is greater than the value of the right operand. If not, then the condition is false. | (x ^^^ y) will give 34, which is 0010 0010 |

~~~ | A condition is true if the value of the right operand is greater than the value of the left operand. If not, then the condition is false. | (~~~x) will give -46, which is 1101 0001 in 2s complement form. |

<<< | A condition is true if the value of the left operand is greater than or equal to the value of the right operand. If not, then the condition is false. | x <<< 2 will give 180 which is 1011 0100 |

>>> | A condition is true if the value of the right operand is greater than and equal to the value of the left operand. If not, then the condition is false. | x >>> 2 will give 11 which is 0000 1011 |

#### Example:

**Output:**

x &&& y - Value of z is 13 x ||| y - Value of z is 47 x ^^^ y - Value of z is 34 ~~~x - Value of z is -46 x <<< 2 - Value of z is 180 x >>> 2 - Value of z is 11

## Operators Precedence

As you can see in the table below, operators and other expression keywords are ranked in order of their precedence in the F# language, where the lowest precedence operator is displayed first, followed by the highest precedence operator.

Operators | Associativity |

as | Right |

when | Right |

| (pipe) | Left |

; | Right |

let | Non associative |

function, fun, match, try | Non associative |

if | Non associative |

→ | Right |

:= | Right |

, | Right |

or, || | Left |

&, && | Left |

< op, >op, =, |op, &op | Left |

&&& , |||, ^^^, ~~~, <<<, >>> | Left |

^ op | Right |

:: | Right |

:?> | Non associative |

– op, +op, (binary) | Left |

* op, /op, %op | Left |

** op | Right |

f x (function application) | Left |

| (pattern match) | Right |

prefix operators (+op, -op, %, %%, &, &&, !op, ~op) | Left |

. | Left |

f(x) | Left |

f<types> | Left |