boole-1, boole-2, boole-and, boole-andc1, boole-andc2, boole-c1, boole-c2, boole-clr, boole-eqv, boole-ior, boole-nand, boole-nor, boole-orc1, boole-orc2, boole-set, boole-xor
Constant Value:
The identity and nature of the values of each of these variables is implementation-dependent, except that it must be distinct from each of the values of the others, and it must be a valid first argument to the function boole.
Description:
Each of these constants has a value which is one of the sixteen possible bit-wise logical operation specifiers.
Examples:
(boole boole-ior 1 16) → 17
(boole boole-and -2 5) → 4
(boole boole-eqv 17 15) → -31
See Also:
booleExpanded Reference: boole-1, boole-2, boole-and, boole-andc1, boole-andc2, boole-c1, boole-c2, boole-clr, boole-eqv, boole-ior, boole-nand, boole-nor, boole-orc1, boole-orc2, boole-set, boole-xor
Constant Values
Each boole- constant is an implementation-dependent integer used as the first argument to boole. Their actual numeric values vary across implementations but their semantics are standardized.
;; Values are implementation-dependent integers
(integerp boole-and)
=> T
(integerp boole-ior)
=> T
(integerp boole-xor)
=> T
Common Boolean Operations with boole
The most frequently used constants are boole-and, boole-ior, and boole-xor, which perform bitwise AND, inclusive OR, and exclusive OR respectively.
(boole boole-and #b1100 #b1010)
=> 8
(boole boole-ior #b1100 #b1010)
=> 14
(boole boole-xor #b1100 #b1010)
=> 6
All 16 Operations
The 16 constants represent all possible two-argument boolean operations. Here they are demonstrated on the same pair of inputs.
(boole boole-clr #b1100 #b1010)
=> 0
(boole boole-set #b1100 #b1010)
=> -1
(boole boole-1 #b1100 #b1010)
=> 12
(boole boole-2 #b1100 #b1010)
=> 10
(boole boole-c1 #b1100 #b1010)
=> -13
(boole boole-c2 #b1100 #b1010)
=> -11
(boole boole-and #b1100 #b1010)
=> 8
(boole boole-ior #b1100 #b1010)
=> 14
(boole boole-xor #b1100 #b1010)
=> 6
(boole boole-eqv #b1100 #b1010)
=> -7
(boole boole-nand #b1100 #b1010)
=> -9
(boole boole-nor #b1100 #b1010)
=> -15
(boole boole-andc1 #b1100 #b1010)
=> 2
(boole boole-andc2 #b1100 #b1010)
=> 4
(boole boole-orc1 #b1100 #b1010)
=> -5
(boole boole-orc2 #b1100 #b1010)
=> -3
Practical Note
In modern Common Lisp code, the dedicated functions logand, logior, logxor, lognot, etc. are preferred over boole because they are more readable and take variable numbers of arguments. The boole function is primarily useful when the operation itself needs to be a parameter.
;; Preferred: use logand, logior, logxor directly
(logand #b1100 #b1010)
=> 8
(logior #b1100 #b1010)
=> 14
;; boole is useful when the operation is a variable
(defun apply-bit-op (op a b)
(boole op a b))
(apply-bit-op boole-and #xFF #x0F)
=> 15
(apply-bit-op boole-xor #xFF #x0F)
=> 240