# LISP - 運算符

• 算術運算

• 比較操作

• 邏輯運算

• 位運算

## 算術運算

Example

+

(+ A B) = 30

-

(- A B)= -10

*

(* A B) = 200

/

(/ B A) = 2

mod,rem

(mod B A ) = 0

incf

(incf A 3) = 13

decf

(decf A 4) = 9

(setq a 10) (setq b 20) (format t "% A + B = ~d" (+ a b)) (format t "% A - B = d" (- a b)) (format t "% A x B = d" (* a b)) (format t "% B / A = d" (/ b a)) (format t "% Increment A by 3 = d" (incf a 3)) (format t "% Decrement A by 4 = ~d" (decf a 4))

A + B = 30 A - B = -10 A x B = 200 B / A = 2 Increment A by 3 = 13 Decrement A by 4 = 9

## 比較操作

Operator

Example

=

(= A B)= true.

/=

(/= A B) =true.

>

(> A B) !=true.

<

(< A B) = true.

>=

(>= A B) !=true.

<=

(<= A B) = true.

max

(max A B) 返回20

min

(min A B) 返回 20

(setq a 10) (setq b 20) (format t "% A = B is ~a" (= a b)) (format t "% A /= B is a" (/= a b)) (format t "% A > B is a" (> a b)) (format t "% A < B is a" (< a b)) (format t "% A >= B is a" (>= a b)) (format t "% A <= B is a" (<= a b)) (format t "% Max of A and B is d" (max a b)) (format t "% Min of A and B is ~d" (min a b))

A = B is NIL
A /= B is T
A > B is NIL
A < B is T
A >= B is NIL
A <= B is T Max of A and B is 20 Min of A and B is 10

## 布爾值邏輯操作

Common Lisp中提供了三種邏輯運算符：AND，OR，而不是運算符的布爾值。假定A=nil，B=5，那麼

and

(and A B) = NIL.

or

(or A B) = 5.

not

(not A) = T.

(setq a 10) (setq b 20) (format t "% A and B is ~a" (and a b)) (format t "% A or B is a" (or a b)) (format t "% not A is a" (not a)) (terpri) (setq a nil) (setq b 5) (format t "% A and B is a" (and a b)) (format t "% A or B is a" (or a b)) (format t "% not A is a" (not a)) (terpri) (setq a nil) (setq b 0) (format t "% A and B is a" (and a b)) (format t "% A or B is a" (or a b)) (format t "% not A is a" (not a)) (terpri) (setq a 10) (setq b 0) (setq c 30) (setq d 40) (format t "% Result of and operation on 10, 0, 30, 40 is a" (and a b c d)) (format t "% Result of and operation on 10, 0, 30, 40 is a" (or a b c d)) (terpri) (setq a 10) (setq b 20) (setq c nil) (setq d 40) (format t "% Result of and operation on 10, 20, nil, 40 is a" (and a b c d)) (format t "% Result of and operation on 10, 20, nil, 40 is ~a" (or a b c d))

A and B is 20 A or B is 10 not A is NIL

A and B is NIL
A or B is 5 not A is T

A and B is NIL
A or B is 0 not A is T Result of and operation on 10, 0, 30, 40 is 40 Result of and operation on 10, 0, 30, 40 is 10 Result of and operation on 10, 20, nil, 40 is NIL Result of and operation on 10, 20, nil, 40 is 10

## 對數位運算

p

q

p and q

p or q

p xor q

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

1

1

Assume if A = 60; and B = 13; now in binary format they will be as follows: A = 0011 1100 B = 0000 1101 ----------------- A and B = 0000 1100 A or B = 0011 1101 A xor B = 0011 0001 not A = 1100 0011

Example

logand

(logand a b)) = 12

logior

(logior a b) = 61

logxor

(logxor a b) = 49

lognor

(lognor a b) = -62,

logeqv

(logeqv a b) = -50

(setq a 60) (setq b 13) (format t "% BITWISE AND of a and b is ~a" (logand a b)) (format t "% BITWISE INCLUSIVE OR of a and b is a" (logior a b)) (format t "% BITWISE EXCLUSIVE OR of a and b is a" (logxor a b)) (format t "% A NOT B is a" (lognor a b)) (format t "% A EQUIVALANCE B is a" (logeqv a b)) (terpri) (terpri) (setq a 10) (setq b 0) (setq c 30) (setq d 40) (format t "% Result of bitwise and operation on 10, 0, 30, 40 is a" (logand a b c d)) (format t "% Result of bitwise or operation on 10, 0, 30, 40 is a" (logior a b c d)) (format t "% Result of bitwise xor operation on 10, 0, 30, 40 is a" (logxor a b c d)) (format t "% Result of bitwise eqivalance operation on 10, 0, 30, 40 is ~a" (logeqv a b c d))

BITWISE AND of a and b is 12 BITWISE INCLUSIVE OR of a and b is 61 BITWISE EXCLUSIVE OR of a and b is 49 A NOT B is -62 A EQUIVALANCE B is -50 Result of bitwise and operation on 10, 0, 30, 40 is 0 Result of bitwise or operation on 10, 0, 30, 40 is 62 Result of bitwise xor operation on 10, 0, 30, 40 is 60 Result of bitwise eqivalance operation on 10, 0, 30, 40 is -61