📜  LISP 中的位运算符

📅  最后修改于: 2022-05-13 01:54:52.807000             🧑  作者: Mango

LISP 中的位运算符

在本文中,我们将讨论 LISP 中的位运算符。这些运算符用于执行数字的各个位的操作。按位ANDNANDORXORNORXNOR的真值表:

aba and b a nand ba or b a xor b a nor b a xnor b
00010011
01011100
11101001
10011100

下面以表格形式列出了 LISP 中的不同位运算符

OperatorSyntaxDescription
logand(logand num1 num2)The operator returns bitwise logical AND of two numbers 
logior(logior num1 num2)The operator returns bitwise Inclusive OR of two numbers
logxor(logxor num1 num2)The operator returns bitwise Exclusive OR of two numbers
lognor(lognor num1 num2)The operator returns bitwise NOT of two numbers
logeqv(logeqv num1 num2)The operator returns bitwise Exclusive NOR of two numbers
logcount(logcount num1)The operator returns the number of ones in the binary representation of the integer

让我们一一了解每个运算符。

  • logand :它将两个数字作为操作数,并对两个数字的每一位进行逻辑与并返回它,如果没有给出操作数,则结果为 -1
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

logand Operation of 5 and 7
    0101
    0111
 ________
=    0101  = 5 (In decimal) 
Lisp
;set value of variable val1 to 5
(setq val1 5)
;set value of variable val2 to 7
(setq val2 7)
  
;; logand operator
(print (logand val1 val2))


Lisp
;set value of variable val1 to 5
(setq val1 5)
;set value of variable val2 to 7
(setq val2 7)
  
;; logior operator
(print (logior val1 val2))


Lisp
;set value of variable val1 to 5
(setq val1 5)
;set value of variable val2 to 7
(setq val2 7)
  
;; logxor operator
(print (logxor val1 val2))


Lisp
;set value of variable val1 to 5
(setq val1 5)
;set value of variable val2 to 7
(setq val2 7)
  
;; lognor operator
(print (lognor val1 val2))


Lisp
;set value of variable val1 to 5
(setq val1 5)
;set value of variable val2 to 7
(setq val2 7)
  
;; logeqv operator
(print (logeqv val1 val2))


Lisp
;set value of variable val1 to 7
(setq val1 7)
  
;; logcount operator
(print (logcount val1))


Lisp
;set value of variable val1 to 10
(setq val1 10)
;set value of variable val2 to 5
(setq val2 5)
  
; arithmetic left shift
(print (ash val1 val2))     
  
; arithmetic right shift 
(print (ash val1 (- val2)))


  • logior:运算符返回传递的参数的按位包含或,如果只传递一个参数,它将返回参数本身
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

logior Operation of 5 and 7
  0101
  0111
 ________
= 0111  = 7 (In decimal) 

语言

;set value of variable val1 to 5
(setq val1 5)
;set value of variable val2 to 7
(setq val2 7)
  
;; logior operator
(print (logior val1 val2))
  • logxor:它返回其参数的按位异或,如果没有传递参数,则返回 0
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

logxor Operation of 5 and 7
  0101
^ 0111
 ________
= 0010  = 2 (In decimal) 

语言

;set value of variable val1 to 5
(setq val1 5)
;set value of variable val2 to 7
(setq val2 7)
  
;; logxor operator
(print (logxor val1 val2))
  • lognor:运算符按位返回其参数的 NOT,如果没有传递参数,则返回 -1
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

lognor Operation of 5 and 7

    0101
    0111
_________    
= -(1000) = -8 in decimal    

语言

;set value of variable val1 to 5
(setq val1 5)
;set value of variable val2 to 7
(setq val2 7)
  
;; lognor operator
(print (lognor val1 val2))
  • logeqv:运算符接受两个参数并对这些参数进行异或(即逻辑等价),如果没有给出参数,则返回 -1
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

logeqv Operation of 5 and 7
  0101
  0111
 ________
= 1101         XNOR is just inversion of XOR

语言

;set value of variable val1 to 5
(setq val1 5)
;set value of variable val2 to 7
(setq val2 7)
  
;; logeqv operator
(print (logeqv val1 val2))
  • logcount:运算符计算整数中的位数,如果数字为正数,则计算 1 位,如果为负数,则计算二进制补码中的 0 位。
a = 7 = 0111 (in Binary)

logcount Operation of 5

= 0111
   ^^^    there are three 1-bits
   
 Hence the logcount of 7 will return 3

例子:

语言

;set value of variable val1 to 7
(setq val1 7)
  
;; logcount operator
(print (logcount val1))

LISP 中的移位运算符

在 LISP 中,对于算术移位,使用了ash函数。如果计数为正,则将位左移,否则,如果计数为负,则右移。

Syntax :    ash number count

例子:

sh 10 5;     arithmetic left shift
ash 10 -5;  arithmetic right shift

语言

;set value of variable val1 to 10
(setq val1 10)
;set value of variable val2 to 5
(setq val2 5)
  
; arithmetic left shift
(print (ash val1 val2))     
  
; arithmetic right shift 
(print (ash val1 (- val2)))

输出 :

320
0