Lessons

Boolean Operations

Welcome to the lesson on Boolean operations in AutoLISP! Boolean operations, named after George Boole, are fundamental in programming, allowing us to make decisions based on conditions. Understanding these operations is essential as they form the basis for most logical structures in a program. By the end of this lesson, you’ll be proficient in using the primary Boolean operators in AutoLISP: and, or, not, Boole, eq, and equal.


Learning Outcomes

By the conclusion of this lesson on Boolean operations in AutoLISP, students will be equipped to:

  • Identify and Understand Boolean Operators: Recognize the significance of and, or, not, Boole, eq, and equal, and discern their functionalities.
  • Apply Boolean Operations: Efficiently use the Boolean operators in diverse programming scenarios, ensuring correct logical outcomes.
  • Interpret Results: Analyze the results of Boolean operations, differentiating between true (T) and false (nil) outcomes.
  • Integrate into Larger Codebases: Effectively incorporate Boolean operations into extensive scripts, optimizing decision-making and logic control.

What are Boolean Operations?

Boolean operations, rooted in Boolean algebra, are named after the mathematician George Boole. These operations serve as the foundational building blocks for formulating logic in programming. Their primary function is to evaluate conditions and produce a binary outcome: either true or false. This binary nature stems from our fundamental understanding of logic, where every statement or proposition can have one of two states.

In the context of AutoLISP, the outcomes of these logical evaluations are represented as T for true and nil for false. The beauty of Boolean operations lies in their ability to shape the decision-making process within a program. By assessing conditions, these operations influence the direction or flow of execution, particularly evident in constructs like conditional statements and loops. As a programmer delves deeper into more complex tasks, they’ll find that these seemingly simple operations underpin a vast array of functionalities, making them indispensable in the world of coding.


Understanding Each Boolean Operator

1. and Operator

The “and” function checks the combined truth value of its given arguments using logical AND. It yields true if all arguments are true; otherwise, it returns false.

Syntax:

(and [expr …])

Example 1:

(and T T) ; This will return T because both conditions are true

Example 2:

(setq p 200 q nil r “apple”)

(and 2.5 p r)
T

(and 2.5 p q r)
nil

(and p r)
T


2. or Operator

The “or” function evaluates a list of expressions using logical OR. It delivers a true result if at least one expression is true; otherwise, it outputs false.

Syntax:

(or [expr …])

Example 1:

(or T nil) ; This will return T because one condition is true

Example 2:

(or nil nil) ; This will return nil because all conditions are false

3. not Operator

The “not” function checks if a given item equates to ‘nil’. Essentially, it confirms the absence of truth in the evaluated item, returning the opposite logical value.

Syntax:

(not item)

Example 1:

(not T) ; This will return nil
Example 2:
(not nil) ; This will return T

4. Boole Function

In AutoLISP, the “boole” function operates as a versatile bitwise Boolean tool. It delves into binary levels, allowing precise manipulations and evaluations on individual bits of data.

Syntax:

(boole operator int1 [int2 …])

Example 1:

(Boole 1 12 10) ; Returns 8 (bitwise AND)

Example 2:

; Logical OR of the values 10 and 6:
(boole 2 10 6)
14; Logical XOR (Exclusive OR) of the values 9 and 5:
(boole 6 9 5)
12; Logical NAND (NOT AND) of the values 7 and 8:
(boole 5 7 8)
7; Logical NOR (NOT OR) of the values 4 and 3:
(boole 7 4 3)
0; Bits set in int2 but not in int3:
(boole 4 5 3)
4

5. eq Function

The eq function evaluates if two expressions share true identity, not just similarity.

Syntax:

(eq symbol1 symbol2)

Example 1:

(setq a ‘apple) (setq b ‘apple) (eq a b) ; This will return T

Example 2:

(setq str1 “hello”)(setq str2 “hello”)
(setq str3 str2); Compare str1 and str3:
(eq str1 str3)
nil
; eq returns nil because str1 and str3, while having the same content, do not refer to the same memory location.; Compare str3 and str2:
(eq str3 str2)
T
; eq returns T because str3 and str2 point to the exact same memory location.(setq num1 100)
(setq num2 100)
(setq num3 num2); Compare num1 and num3:
(eq num1 num3)
nil
; eq returns nil because num1 and num3, even though they have the same value, do not refer to the same memory location.; Compare num3 and num2:
(eq num3 num2)
T
; eq returns T because num3 and num2 point to the exact same memory location.

6. equal Function

The equal function assesses if two expressions hold the same value, ensuring equivalence.

Syntax:

(equal expr1 expr2 [fuzz])

fuzz

Type: Integer or Real

A numeric value specifying the maximum allowable difference between expr1 and expr2 for them to be deemed equivalent.

Example 1:

(equal ‘(1 2 3) ‘(1 2 3)) ; This will return T

Example 2:

(equal “apple” “orange”) ; This will return nil

Example 3:

(setq arr1 ‘(x y z))(setq arr2 ‘(x y z))
(setq arr3 arr2)
(setq m 2.345678)
(setq n 2.345679); Compare arr1 to arr3:
(equal arr1 arr3)
T; Compare arr3 to arr2:
(equal arr3 arr2)
T; Compare m to n:
(equal m n)
nil; The m and n variables differ by .000001. Compare m to n, with fuzz argument of .000001:
(equal m n 0.000001)
T
; The m and n variables differ by an amount equal to the specified fuzz factor, so equal considers the variables equal.

(setq strA “AutoLISP”)
(setq strB “Autolisp”)
(setq strC strB)

; Compare strA to strC:
(equal strA strC)
nil

; Strings are case-sensitive, but using the equal function with an additional argument for case-insensitivity:
(equal strA strC T)
T

; With the case-insensitive argument, strA and strC are considered equal despite the difference in case.

Conclusion

Boolean operations are the bedrock of logic in programming. Mastering these operations in AutoLISP will not only bolster your coding capabilities but will also provide a sturdy foundation for more intricate logical constructs as you advance in your programming endeavors. Continuously practice using these operators, and always explore various scenarios to solidify your understanding. Your journey in AutoLISP is just beginning, and these Boolean tools will be invaluable companions along the way.