# 关于算术运算符About Arithmetic Operators

## 详细说明LONG DESCRIPTION

PowerShell 支持以下算术运算符：PowerShell supports the following arithmetic operators:

+ 添加整数;各个Adds integers; concatenates 6 + 2

@(1, "one") + @(2.0, "two")
@{"one" = 1} + @{"two" = 2}
- 从一个值中减去另一个值Subtracts one value from another 6 - 2
valuevalue
- 使数字成为负数Makes a number a negative number -6
* 数字或复制字符串相乘Multiply numbers or copy strings 6 * 2

/ 两个值相除。Divides two values. 6 / 2
% 取模-返回剩余的Modulus - returns the remainder of 7 % 2

-带-band 位与Bitwise AND 5 -band 3
-bnot-bnot 按位“非”Bitwise NOT -bnot 5
-bor-bor 按位“或”Bitwise OR 5 -bor 0x03
-bxor-bxor 按位“异或”Bitwise XOR 5 -bxor 3
-shl-shl 将位向左移动Shifts bits to the left 102 -shl 2
-shr-shr 将位向右移位Shifts bits to the right 102 -shr 2

## 运算符优先级OPERATOR PRECEDENCE

PowerShell 按以下顺序处理算术运算符：PowerShell processes arithmetic operators in the following order:

11 () 括号Parentheses
22 - 对于负数或一元运算符For a negative number or unary operator
33 *, /, %*, /, % 对于乘法和除法For multiplication and division
44 +, -+, - 对于加法和减法For addition and subtraction
55 -band, -bnot-band, -bnot 对于按位运算For bitwise operations
55 -bor, -bxor-bor, -bxor 对于按位运算For bitwise operations
55 -shr, -shl-shr, -shl 对于按位运算For bitwise operations

3+6/3*4 11
3+6/(3*4) 3.5
(3+6)/3*4 12

PowerShell 计算表达式的顺序可能不同于你使用的其他编程和脚本语言。The order in which PowerShell evaluates expressions might differ from other programming and scripting languages that you have used. 下面的示例演示了复杂的赋值语句。The following example shows a complicated assignment statement.

\$a = 0
\$b = @(1,2)
\$c = @(-1,-2)

\$b[\$a] = \$c[\$a++]

\$a = 0
\$b = @(1,2)
\$c = @(-1,-2)

\$tmp = \$c[\$a]
\$a = \$a + 1
\$b[\$a] = \$tmp

## 相除和舍入DIVISION AND ROUNDING

[int]( 5 / 2 ) 2
[int]( 7 / 2 ) 4

[int][Math]::Round(5 / 2,[MidpointRounding]::AwayFromZero) 3
[int][Math]::Ceiling(5 / 2) 3
[int][Math]::Floor(5 / 2) 2

## 添加非数值类型和相乘ADDING AND MULTIPLYING NON-NUMERIC TYPES

\$a = 1,2,3
\$b = "A","B","C"
\$a + \$b
1
2
3
A
B
C

"file" + 16 file16
\$array + 16 1,2,3,161,2,3,16
\$array + "file" 1,2,3,file1,2,3,file
\$array * 2 1,2,3,1,2,31,2,3,1,2,3
"file" * 3 filefilefile

"file" + 16 file16
16 + "file" Cannot convert value "file" to type "System.Int32".
Error: "Input string was not in a correct format."
At line:1 char:1
+ 16 + "file"+ 16 + "file"`

\$hash1 = @{a=1; b=2; c=3}
\$hash2 = @{c1="Server01"; c2="Server02"}
\$hash1 + \$hash2
Name                           Value
----                           -----
c2                             Server02
a                              1
b                              2
c1                             Server01
c                              3

\$hash1 = @{a=1; b=2; c=3}
\$hash2 = @{c1="Server01"; c="Server02"}
\$hash1 + \$hash2
Item has already been added. Key in dictionary: 'c'  Key being added: 'c'
At line:3 char:1
+ \$hash1 + \$hash2
+ ~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException

\$array1 = @(0, "Hello World", [datetime]::Now)
\$hash1 = @{a=1; b=2}
\$array2 = \$array1 + \$hash1
\$array2
0
Hello World

Monday, June 12, 2017 3:05:46 PM

Key   : a
Value : 1
Name  : a

Key   : b
Value : 2
Name  : b

\$hash1 + 2
A hash table can only be added to another hash table.
At line:3 char:1
+ \$hash1 + 2

\$array = @()
(0..9).foreach{ \$array += \$_ }
\$array
0
1
2

## 用于容纳结果的类型转换TYPE CONVERSION TO ACCOMMODATE RESULT

PowerShell 会自动选择最能表达结果的 .NET Framework 数值类型，而不会丢失精度。PowerShell automatically selects the .NET Framework numeric type that best expresses the result without losing precision. 例如：For example:

2 + 3.1

(2). GetType().FullName
(2 + 3.1).GetType().FullName
5.1
System.Int32
System.Double

(512MB).GetType().FullName
(512MB * 512MB).GetType().FullName
System.Int32
System.Double

([int32]::minvalue + [uint32]::maxvalue).gettype().fullname
System.Int64

System.Decimal类型为异常。The System.Decimal type is an exception. 如果任一操作数具有 Decimal 类型，则结果将为 Decimal 类型。If either operand has the Decimal type, the result will be of the Decimal type. 如果结果对 Decimal 类型来说太大，则不会将其强制转换为 Double 类型。If the result is too large for the Decimal type, it will not be cast to Double. 相反，会产生错误。Instead, an error results.

[Decimal]::maxvalue 79228162514264337593543950335
[Decimal]::maxvalue + 1 Value was either too large or too small for a
Decimal.

\$intA = 6
\$intB = 4
\$intA + \$intB
10
\$a = "Power"
\$b = "Shell"
\$a + \$b
PowerShell

## 算术运算符和命令ARITHMETIC OPERATORS AND COMMANDS

(get-date) + (new-timespan -day 1)

Get-Process | Where-Object { (\$_.ws * 2) -gt 50mb }
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
1896      39    50968      30620   264 1,572.55   1104 explorer
12802      78   188468      81032   753 3,676.39   5676 OUTLOOK
660       9    36168      26956   143    12.20    988 PowerShell
561      14     6592      28144   110 1,010.09    496 services
3476      80    34664      26092   234 ...45.69    876 svchost
967      30    58804      59496   416   930.97   2508 WINWORD

## 位运算符Bitwise Operators

PowerShell 支持标准按位运算符，包括按位与 (-bAnd) 、 (和) 的非独占和独占按位或运算符 -bOr-bXor 以及按位 "非 () -bNotPowerShell supports the standard bitwise operators, including bitwise-AND (-bAnd), the inclusive and exclusive bitwise-OR operators (-bOr and -bXor), and bitwise-NOT (-bNot).

PowerShell 支持以下按位运算符。PowerShell supports the following bitwise operators.

-band 位与Bitwise AND 10 -band 3 22
-bor 按位 OR (包含) Bitwise OR (inclusive) 10 -bor 3 1111
-bxor 按位或 (独占) Bitwise OR (exclusive) 10 -bxor 3 99
-bnot 按位“非”Bitwise NOT -bNot 10 -11-11
-shl 左移Shift-left 102 -shl 2 408408
-shr 向右移动Shift-right 102 -shr 1 5151

1010      (10)
0011      ( 3)
--------------  bAND
0010      ( 2)

1010      (10)
0011      ( 3)
--------------  bOR (inclusive)
1011      (11)

1010      (10)
0011      ( 3)
--------------  bXOR (exclusive)
1001      ( 9)

-bNot 10
-11
0000 0000 0000 1010  (10)
------------------------- bNOT
1111 1111 1111 0101  (-11, xfffffff5)

21 -shl 0 2121 0001 01010001 0101
21 -shl 1 4242 0010 10100010 1010
21 -shl 2 8484 0101 01000101 0100

21 -shr 0 2121 0001 01010001 0101 0x150x15
21 -shr 1 1010 0000 10100000 1010 0x0A0x0A
21 -shr 2 55 0000 01010000 0101 0x050x05
21 -shr 31 00 0000 00000000 0000 0x000x00
21 -shr 32 2121 0001 01010001 0101 0x150x15
21 -shr 64 2121 0001 01010001 0101 0x150x15
21 -shr 65 1010 0000 10100000 1010 0x0A0x0A
21 -shr 66 55 0000 01010000 0101 0x050x05
[int]::MaxValue -shr 1 10737418231073741823 0x3FFFFFFF0x3FFFFFFF
[int]::MinValue -shr 1 -1073741824-1073741824 0xC00000000xC0000000
-1 -shr 1 -1-1 0xFFFFFFFF0xFFFFFFFF