📜  Erlang-运营商

📅  最后修改于: 2020-11-04 05:49:25             🧑  作者: Mango


运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。

Erlang具有以下类型的运算符-

  • 算术运算运算符
  • 关系运算符
  • 逻辑运算符
  • 按位运算符

算术运算符

Erlang语言支持普通的算术运算运算符,就像任何一种语言一样。以下是Erlang中可用的算术运算运算符。

显示范例

Operator Description Example
+ Addition of two operands 1 + 2 will give 3
Subtracts second operand from the first 1 – 2 will give -1
* Multiplication of both operands 2 * 2 will give 4
/ Division of numerator by denominator 2 / 2 will give 1
rem Remainder of dividing the first number by the second 3 rem 2 will give 1
div The div component will perform the division and return the integer component. 3 div 2 will give 1

关系运算符

关系运算符允许比较对象。以下是Erlang中可用的关系运算符。

显示范例

Operator Description Example
== Tests the equality between two objects 2 = 2 will give true
/= Tests the difference between two objects 3 /= 2 will give true
< Checks to see if the left object is less than the right operand. 2 < 3 will give true
=< Checks to see if the left object is less than or equal to the right operand. 2 =<3 will give true
> Checks to see if the left object is greater than the right operand. 3 > 2 will give true
>= Checks to see if the left object is greater than or equal to the right operand. 3 >= 2 will give true

逻辑运算符

这些逻辑运算符用于评估布尔表达式。以下是Erlang中可用的逻辑运算符。

显示范例

Operator Description Example
or This is the logical “or” operator true or true will give true
and This is the logical “and” operator True and false will give false
not This is the logical “not” operator not false will give true
xor This is the logical exclusive “xor” operator True xor false will give true

按位运算符

Erlang提供了四个按位运算运算符。以下是Erlang中可用的按位运算运算符。

显示范例

Sr.No. Operator & Description
1

band

This is the bitwise “and” operator

2

bor

This is the bitwise “or” operator

3

bxor

This is the bitwise “xor” or Exclusive or operator

4

bnot

This is the bitwise negation operator

以下是展示这些运算符的真值表-

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

运算符优先级

下表显示了运算符优先级的运算符二郎神与他们一起的关联优先级的降序。运算符优先级和关联性用于确定非括号表达式中的评估顺序。

Operators Associativity
:
#
bnot,not
/,*,div,rem,band,and Left associative
+,-,bor,bxor,or,xor Left associative
==,/=,=<,<,>=,>