 Open in App
Not now

# 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

Different bitwise operators in LISP are listed below in the tabular form

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```

My Personal Notes arrow_drop_up
Related Articles