# D語言運算符

• 算術運算符

• 關係運算符

• 邏輯運算符

• 位運算符

• 賦值運算符

• 其它運算符

+

A + B = 30

-

A - B = -10

*

A * B = 200

/

B / A = 2

%

B % A = 0

++

A++ = 11

--

A-- = 9

## 關係運算符

==

(A == B) =not true.

!=

(A != B) =true.

>

(A > B) =not true.

<

(A < B) =true.

>=

(A >= B) = not true.

<=

(A <= B) =true.

## 邏輯運算符

&&

(A && B) = false.

||

(A || B) = true.

!

!(A && B) =true.

## 位運算符

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

A = 0011 1100

B = 0000 1101

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

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

&

(A & B) = 12,  0000 1100

|

(A | B) = 61, 0011 1101

^

(A ^ B) = 49,  0011 0001

~

(~A ) = -61,  1100 0011

<<

A << 2 = 240 , 1111 0000

>>

A >> 2 = 15  0000 1111

## 賦值運算符

=

C = A + B will assign value of A + B into C

+=

C += A is equivalent to C = C + A

-=

C -= A is equivalent to C = C - A

*=

C *= A is equivalent to C = C * A

/=

C /= A is equivalent to C = C / A

%=

C %= A is equivalent to C = C % A

<<=

C <<= 2 is same as C = C << 2

>>=

C >>= 2 is same as C = C >> 2

&=

C &= 2 is same as C = C & 2

^=

C ^= 2 is same as C = C ^ 2

|=

OR運算和賦值運算符

C |= 2 is same as C = C | 2

## 其它運算符↦的sizeof＆三元

sizeof()

sizeof(a),，其中a是整數，將返回4。

&

&a; 將給變量的實際地址。

*

*a; 將爲指針變量。

? :

## D語言運算符優先級

Postfix

() [] -> . ++ - -

Left to right

Unary

+ - ! ~ ++ - - (type)* & sizeof

Right to left

Multiplicative

* / %

Left to right

+ -

Left to right

Shift

<< >>

Left to right

Relational

< <= > >=

Left to right

Equality

== !=

Left to right

Bitwise AND

Left to right

Bitwise XOR

Left to right

Bitwise OR

Left to right

Logical AND

&&

Left to right

Logical OR

||

Left to right

Conditional

?:

Right to left

Assignment

= += -= *= /= %=>>= <<= &= ^= |=

Right to left

Comma

Left to right