📅  最后修改于: 2020-11-04 08:22:08             🧑  作者: Mango
运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。 elixir提供了很多运算符。它们分为以下类别-
下表显示了Elixir语言支持的所有算术运算运算符。假设变量A持有10,变量B持有20,则-
Operator | Description | Example |
---|---|---|
+ | Adds 2 numbers. | A + B will give 30 |
– | Subtracts second number from first. | A-B will give -10 |
* | Multiplies two numbers. | A*B will give 200 |
/ | Divides first number from second. This casts the numbers in floats and gives a float result | A/B will give 0.5. |
div | This function is used to get the quotient on division. | div(10,20) will give 0 |
rem | This function is used to get the remainder on division. | rem(A, B) will give 10 |
在药剂比较运算符大多是通用于大多数其他语言所提供的。下表中药剂总结了运算符。假设变量A持有10,变量B持有20,则-
Operator | Description | Example |
---|---|---|
== | Checks if value on left is equal to value on right(Type casts values if they are not the same type). | A == B will give false |
!= | Checks if value on left is not equal to value on right. | A != B will give true |
=== | Checks if type of value on left equals type of value on right, if yes then check the same for value. | A === B will give false |
!== | Same as above but checks for inequality instead of equality. | A !== B will give true |
> | Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. | A > B will give false |
< | Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. | A < B will give true |
>= | Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. | A >= B will give false |
<= | Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true. | A <= B will give true |
Elixir提供6个逻辑运算符:和,或不,&&,||和!前三个,并且还是不严格的布尔运算符,这意味着他们希望自己的第一个参数是一个布尔值。非布尔参数将引发错误。而下三个&&,||和!是非严格的,不要求我们严格地将布尔值作为第一个值。他们的工作方式与严格的同行相同。假设变量A成立,变量B持有20,则-
Operator | Description | Example |
---|---|---|
and | Checks if both values provided are truthy, if yes then returns the value of second variable. (Logical and). | A and B will give 20 |
or | Checks if either value provided is truthy. Returns whichever value is truthy. Else returns false. (Logical or). | A or B will give true |
not | Unary operator which inverts the value of given input. | not A will give false |
&& | Non-strict and. Works same as and but does not expect first argument to be a Boolean. | B && A will give 20 |
|| | Non-strict or. Works same as or but does not expect first argument to be a Boolean. | B || A will give true |
! | Non-strict not. Works same as not but does not expect the argument to be a Boolean. | !A will give false |
注意−和,或, &&和|| ||是短路运算符。这意味着,如果第一个参数是假的,那么就不会在第二个进一步的检查。并且,如果or的第一个参数为true,则不会检查第二个参数。例如,
false and raise("An error")
#This won't raise an error as raise function wont get executed because of short
#circuiting nature of and operator
按位运算符对位进行运算并逐位执行操作。 Elixir提供Bitwise模块作为Bitwise包的一部分,因此,要使用这些模块,您需要使用bitwise模块。要使用它,请在您的shell中输入以下命令-
use Bitwise
对于以下示例,假设A为5,B为6-
Operator | Description | Example |
---|---|---|
&&& | Bitwise and operator copies a bit to result if it exists in both operands. | A &&& B will give 4 |
||| | Bitwise or operator copies a bit to result if it exists in either operand. | A ||| B will give 7 |
>>> | Bitwise right shift operator shifts first operand bits to the right by the number specified in second operand. | A >>> B will give 0 |
<<< | Bitwise left shift operator shifts first operand bits to the left by the number specified in second operand. | A <<< B will give 320 |
^^^ | Bitwise XOR operator copies a bit to result only if it is different on both operands. | A ^^^ B will give 3 |
~~~ | Unary bitwise not inverts the bits on the given number. | ~~~A will give -6 |
除上述运算符,Elixir还提供一系列其他运算符,例如串联运算符,匹配运算符,引脚运算符,管道运算符,字符串匹配运算符,代码点运算符,捕获运算符,三元运算符,这使其成为一门强大的语言。