- LISP 教程
- LISP - 首頁
- LISP - 概述
- LISP - 環境
- LISP - 程式結構
- LISP - 基本語法
- LISP - 資料型別
- LISP - 宏
- LISP - 變數
- LISP - 常量
- LISP - 運算子
- LISP - 決策
- LISP - 迴圈
- LISP - 函式
- LISP - 謂詞
- LISP - 數字
- LISP - 字元
- LISP - 陣列
- LISP - 字串
- LISP - 序列
- LISP - 列表
- LISP - 符號
- LISP - 向量
- LISP - 集合
- LISP - 樹
- LISP - 雜湊表
- LISP - 輸入 & 輸出
- LISP - 檔案 I/O
- LISP - 結構體
- LISP - 包
- LISP - 錯誤處理
- LISP - CLOS
- LISP 有用資源
- Lisp - 快速指南
- Lisp - 有用資源
- Lisp - 討論
Lisp - 位運算子
位運算子作用於位,並執行逐位運算。位與、位或和位異或運算的真值表如下所示:
| 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 |
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
LISP 支援的位運算子列在下表中。假設變數 A 的值為 60,變數 B 的值為 13,則:
| 運算子 | 描述 | 示例 |
|---|---|---|
| logand | 返回其引數的按位邏輯與。如果未給出引數,則結果為 -1,這是此運算的恆等式。 | (logand a b) 將給出 12 |
| logior | 返回其引數的按位邏輯包含或。如果未給出引數,則結果為零,這是此運算的恆等式。 | (logior a b) 將給出 61 |
| logxor | 返回其引數的按位邏輯異或。如果未給出引數,則結果為零,這是此運算的恆等式。 | (logxor a b) 將給出 49 |
| lognor | 返回其引數的按位非。如果未給出引數,則結果為 -1,這是此運算的恆等式。 | (lognor a b) 將給出 -62, |
| logeqv | 返回其引數的按位邏輯等價(也稱為異或非)。如果未給出引數,則結果為 -1,這是此運算的恆等式。 | (logeqv a b) 將給出 -50 |
示例 - logand、logior 運算子
建立一個名為 main.lisp 的新原始碼檔案,並在其中鍵入以下程式碼。這裡我們使用logand、logior運算子來檢查各種場景。
main.lisp
; set a as 60 (setq a 60) ; set b as 13 (setq b 13) ; print logand of a and b (format t "~% BITWISE AND of a and b is ~a" (logand a b)) ; print logand of a or b (format t "~% BITWISE INCLUSIVE OR of a and b is ~a" (logior a b)) ; terminate printing to print in new line (terpri) ; assign variables new values (setq a 10) (setq b 0) (setq c 30) (setq d 40) ; print logand of all four variables (format t "~% Result of bitwise and operation on 10, 0, 30, 40 is ~a" (logand a b c d)) ; print logor of all four variables (format t "~% Result of bitwise or operation on 10, 0, 30, 40 is ~a" (logior a b c d))
單擊“執行”按鈕或鍵入 Ctrl+E 時,LISP 會立即執行它,並返回以下結果:
BITWISE AND of a and b is 12 BITWISE INCLUSIVE OR of a and b is 61 Result of bitwise and operation on 10, 0, 30, 40 is 0 Result of bitwise or operation on 10, 0, 30, 40 is 62
示例 - logxor、lognor 運算子
更新名為 main.lisp 的原始碼檔案,並在其中鍵入以下程式碼。這裡我們使用logxor、lognor運算子來檢查各種場景。
main.lisp
; set a as 60 (setq a 60) ; set b as 13 (setq b 13) ; print logxor of a and b (format t "~% BITWISE EXCLUSIVE OR of a and b is ~a" (logxor a b)) ; print lognor of a and b (format t "~% A NOT B is ~a" (lognor a b)) ; terminate printing to print in new line (terpri) ; assign variables new values (setq a 10) (setq b 0) (setq c 30) (setq d 40) ; print logxor of all four variables (format t "~% Result of bitwise xor operation on 10, 0, 30, 40 is ~a" (logxor a b c d)) ; print logeqv of all four variables (format t "~% Result of bitwise eqivalance operation on 10, 0, 30, 40 is ~a" (logeqv a b c d))
單擊“執行”按鈕或鍵入 Ctrl+E 時,LISP 會立即執行它,並返回以下結果:
BITWISE EXCLUSIVE OR of a and b is 49A NOT B 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
示例 - logeqv 運算子
更新名為 main.lisp 的原始碼檔案,並在其中鍵入以下程式碼。這裡我們使用logeqv運算子來檢查場景。
main.lisp
; set a as 60 (setq a 60) ; set b as 13 (setq b 13) ; print logeqv of a and b (format t "~% A EQUIVALANCE B is ~a" (logeqv a b)) ; terminate printing to print in new line (terpri) ; assign variables new values (setq a 10) (setq b 0) (setq c 30) (setq d 40) ; print logeqv of all four variables (format t "~% Result of bitwise eqivalance operation on 10, 0, 30, 40 is ~a" (logeqv a b c d))
單擊“執行”按鈕或鍵入 Ctrl+E 時,LISP 會立即執行它,並返回以下結果:
A EQUIVALANCE B is -50 Result of bitwise eqivalance operation on 10, 0, 30, 40 is -61
lisp_operators.htm
廣告