Bitwise Operators in LISP
Last Updated :
29 Oct, 2021
In this article, we will discuss the Bitwise operators in LISP. These operators are used to perform the manipulation of individual bits of a number. The truth table for bitwise AND, NAND, OR, XOR, NOR, & XNOR:
a | b | a and b | a nand b | a or b | a xor b | a nor b | a xnor b |
---|
0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 |
---|
0 | 1 | 0 | 1 | 1 | 1 | 0 | 0 |
---|
1 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
---|
1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
---|
Different bitwise operators in LISP are listed below in the tabular form
Operator | Syntax | Description |
---|
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 |
---|
Let’s understand each operator one by one.
- logand: It takes two numbers as operands and does logical AND on every bit of two numbers and returns it, If no operands are given then the result is -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))
- logior: The operator returns bitwise Inclusive OR of the arguments that are passed, If only a single argument is passed it will return the argument itself
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
logior Operation of 5 and 7
0101
0111
________
= 0111 = 7 (In decimal)
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))
- logxor: It returns the bitwise Exclusive OR of its arguments, if no arguments are passed it returns 0
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
logxor Operation of 5 and 7
0101
^ 0111
________
= 0010 = 2 (In decimal)
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))
- lognor: The operator returns bitwise NOT of its arguments, if no arguments are passed it returns -1
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
lognor Operation of 5 and 7
0101
0111
_________
= -(1000) = -8 in decimal
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))
- logeqv: The operator takes two arguments and does Exclusive Nor(i.e. Logical Equivalence) of those arguments, if no arguments are given then it returns -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
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))
- logcount: The operator counts the number of bits in an integer, If the number is positive then 1-bits are counted, if it's negative then 0-bits in two's complement are counted.
a = 7 = 0111 (in Binary)
logcount Operation of 5
= 0111
^^^ there are three 1-bits
Hence the logcount of 7 will return 3
Example:
Lisp
;set value of variable val1 to 7
(setq val1 7)
;; logcount operator
(print (logcount val1))
Shift Operators in LISP
In LISP, for an arithmetic shift, the ash function is used. If the count is positive it shifts the bits to left, else if the count is negative it does the right shift.
Syntax : ash number count
Example:
sh 10 5; arithmetic left shift
ash 10 -5; arithmetic right shift
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)))
Output :
320
0
Similar Reads
Bitwise Operators in C++ In C+, Bitwise Operators are the operators that are used to perform bit-level operations on the integers. While performing these operations, integers are considered as sequences of binary digits. These operators are useful for low-level programming, system programming, and optimizing performance.C++
6 min read
Operators in LISP Operators are the foundation of any programming language. Thus the functionality of the LISP programming language is incomplete without the use of operators. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can
5 min read
Arithmetic Operators in LISP Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division. There are 7 arithmetic operators in LISP that are listed in the below table: OperatorSyntaxDescriptionAddition Operator(+)+ num1 num2Add the two numbersSubtraction Operator(-)-
2 min read
Comparison Operators in LISP In this article, we will discuss the comparison operators in LISP. These operators are used to compare numbers by taking two or more operands. Note: This will work only on numbers, Different comparison operators are:OperatorSyntaxNameDescription== operand1 operand2equal toThis operator checks if the
4 min read
Bitwise NOT operator in Golang Bitwise NOT operator in the programming world usually takes one number and returns the inverted bits of that number as shown below: Bitwise NOT of 1 = 0 Bitwise NOT of 0 = 1 Example: Input : X = 010101 Output : Bitwise NOT of X = 101010 But Golang doesn't have any specified unary Bitwise NOT(~) or y
2 min read
Bitwise OR Operator (|) in Programming In programming, Bitwise Operators play a crucial role in manipulating individual bits of data. One of the fundamental bitwise operators is the Bitwise OR operator (|). In this article, weâll discuss the Bitwise OR operator, its syntax, properties, applications, and optimization techniques, and concl
5 min read