F# Tutorial on F# Operators

an operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. f# is rich in built-in operators and provides the following types of operators −

  • arithmetic operators
  • comparison operators
  • boolean operators
  • bitwise operators

arithmetic operators

the following table shows all the arithmetic operators supported by f# language. assume variable a holds 10 and variable b holds 20 then −

show example

operator description example
+ adds two operands a + b will give 30
- subtracts second operand from the first a - b will give -10
* multiplies both operands a * b will give 200
/ divides numerator by de-numerator b / a will give 2
% modulus operator and remainder of after an integer division b % a will give 0
** exponentiation operator, raises an operand to the power of another b**a will give 2010

comparison operators

the following table shows all the comparison operators supported by f# language. these binary comparison operators are available for integral and floating-point types. these operators return values of type bool.

assume variable a holds 10 and variable b holds 20, then −

show example

operator description example
= checks if the values of two operands are equal or not, if yes then condition becomes true. (a == b) is not true.
<> checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (a <> b) is true.
> checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (a > b) is not true.
< checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (a < b) is true.
>= checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (a >= b) is not true.
<= checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (a <= b) is true.

boolean operators

the following table shows all the boolean operators supported by f# language. assume variable a holds true and variable b holds false, then −

show example

operator description example
&& called boolean and operator. if both the operands are non-zero, then condition becomes true. (a && b) is false.
|| called boolean or operator. if any of the two operands is non-zero, then condition becomes true. (a || b) is true.
not called boolean not operator. use to reverses the logical state of its operand. if a condition is true then logical not operator will make false. not (a && b) is true.

bitwise operators

bitwise operators work on bits and perform bit-by-bit operation. the truth tables for &&& (bitwise and), ||| (bitwise or), and ^^^ (bitwise exclusive or) are as follows −

show example

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

assume if a = 60; and b = 13; now in binary format they will be as follows −

a = 0011 1100

b = 0000 1101

-----------------

a&&&b = 0000 1100

a|||b = 0011 1101

a^^^b = 0011 0001

~~~a = 1100 0011

the bitwise operators supported by f# language are listed in the following table. assume variable a holds 60 and variable b holds 13, then −

operator description example
&&& binary and operator copies a bit to the result if it exists in both operands. (a &&& b) will give 12, which is 0000 1100
||| binary or operator copies a bit if it exists in either operand. (a ||| b) will give 61, which is 0011 1101
^^^ binary xor operator copies the bit if it is set in one operand but not both. (a ^^^ b) will give 49, which is 0011 0001
~~~ binary ones complement operator is unary and has the effect of 'flipping' bits. (~~~a) will give -61, which is 1100 0011 in 2's complement form.
<<< binary left shift operator. the left operands value is moved left by the number of bits specified by the right operand. a <<< 2 will give 240 which is 1111 0000
>>> binary right shift operator. the left operands value is moved right by the number of bits specified by the right operand. a >>> 2 will give 15 which is 0000 1111

operators precedence

the following table shows the order of precedence of operators and other expression keywords in the f# language, from lowest precedence to the highest precedence.

show example

operator associativity
as right
when right
| (pipe) left
; right
let non associative
function, fun, match, try non associative
if non associative
right
:= right
, non associative
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 (&plus;op, -op, %, %%, &, &&, !op, ~op) left
. left
f(x) left
f<types> left