📜  批处理脚本-运算符

📅  最后修改于: 2020-11-22 17:49:41             🧑  作者: Mango


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

在批处理脚本中,可以使用以下类型的运算符。

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

算术运算符

批处理脚本语言支持普通算术运算运算符,就像任何语言一样。以下是可用的算术运算运算符。

显示范例

Operator Description Example
+ Addition of two operands 1 + 2 will give 3
Subtracts second operand from the first 2 − 1 will give 1
* Multiplication of both operands 2 * 2 will give 4
/ Division of the numerator by the denominator 3 / 2 will give 1.5
% Modulus operator and remainder of after an integer/float division 3 % 2 will give 1

关系运算符

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

显示范例

Operator Description Example
EQU Tests the equality between two objects 2 EQU 2 will give true
NEQ Tests the difference between two objects 3 NEQ 2 will give true
LSS Checks to see if the left object is less than the right operand 2 LSS 3 will give true
LEQ Checks to see if the left object is less than or equal to the right operand 2 LEQ 3 will give true
GTR Checks to see if the left object is greater than the right operand 3 GTR 2 will give true
GEQ Checks to see if the left object is greater than or equal to the right operand 3 GEQ 2 will give true

逻辑运算符

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

批处理语言配备了完整的布尔逻辑运算符,例如AND,OR,XOR,但仅适用于二进制数。 TRUE或FALSE都没有任何值。可用于条件的唯一逻辑运算符是NOT运算符。

显示范例

Operator Description
AND This is the logical “and” operator
OR This is the logical “or” operator
NOT This is the logical “not” operator

赋值运算符

批处理脚本语言还提供赋值运算符。以下是可用的赋值运算符。

显示范例

Operator Description Example
+= This adds right operand to the left operand and assigns the result to left operand

Set /A a = 5

a += 3

Output will be 8

-= This subtracts the right operand from the left operand and assigns the result to the left operand

Set /A a = 5

a -= 3

Output will be 2

*= This multiplies the right operand with the left operand and assigns the result to the left operand

Set /A a = 5

a *= 3

Output will be 15

/= This divides the left operand with the right operand and assigns the result to the left operand

Set /A a = 6

a/ = 3

Output will be 2

%= This takes modulus using two operands and assigns the result to the left operand

Set /A a = 5

a% = 3

Output will be 2

按位运算符

批处理脚本中也可以使用按位运算符。以下是可用的运算符。

显示范例

Operator Description
& This is the bitwise “and” operator
| This is the bitwise “or” operator
^ This is the bitwise “xor” or Exclusive or 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