Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lisp - Logical operators

I am new to Lisp so this might be so simple but I am curious to know about this in any case.

I am familiar with logical operators like AND and OR but lisp does not seem to behave as expected.

For example, For (and 1 8)

Expected:

1 => 0 0 0 1
8 => 1 0 0 0
(and 1 8) => 0 0 0 0

Received: So, the answer should have been 0 ...but instead it is 8

Questions:

  1. How is this calculation done in LISP?
  2. Is Logical operators fundamentally different in LISP?
like image 623
Naveen Dennis Avatar asked Apr 12 '17 20:04

Naveen Dennis


1 Answers

In Common Lisp, AND and OR operate on boolean values, not binary digits. NIL is the only false value, anything else is considered true.

To operate on the binary representation of numbers, use LOGAND, LOGIOR, etc. These are all documented at http://clhs.lisp.se/Body/f_logand.htm.

(logand 1 8) ==> 0
like image 107
Barmar Avatar answered Sep 22 '22 02:09

Barmar