Show 例子 4
优质
小牛编辑
135浏览
2023-12-01
按位运算符处理位并执行逐位运算。 按位和,或和xor运算的真值表如下 -
p | q | p和q | p或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
LISP支持的Bitwise运算符如下表所示。 假设变量A保持60,变量B保持13,则 -
操作者 | 描述 | 例 |
---|---|---|
logand | 这将返回其参数的逐位逻辑AND。 如果没有给出参数,则结果为-1,这是此操作的标识。 | (logand ab))将给出12 |
logior | 这将返回其参数的逐位逻辑INCLUSIVE OR。 如果没有给出参数,则结果为零,这是此操作的标识。 | (logior ab)将给出61 |
logxor | 这将返回其参数的逐位逻辑EXCLUSIVE OR。 如果没有给出参数,则结果为零,这是此操作的标识。 | (logxor ab)将给49 |
lognor | 这将返回其参数的逐位NOT。 如果没有给出参数,则结果为-1,这是此操作的标识。 | (lognor ab)将给出-62, |
logeqv | 这将返回其参数的逐位逻辑EQUIVALENCE(也称为异或)。 如果没有给出参数,则结果为-1,这是此操作的标识。 | (logeqv ab)将给予-50 |
例子 (Example)
创建一个名为main.lisp的新源代码文件,并在其中键入以下代码。
(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))
单击“执行”按钮或键入Ctrl + E时,LISP立即执行它,返回的结果为 -
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