bit, sbit
bit, sbit Accessor
Syntax:
bit bit-array &rest subscripts → bit
sbit bit-array &rest subscripts → bit
(setf (bit bit-array &rest subscripts**)** new-bit**)**
(setf (sbit bit-array &rest subscripts**)** new-bit**)**
Arguments and Values:
bit-array—for bit, a bit array; for sbit, a simple bit array.
subscripts—a list of valid array indices for the bit-array.
bit—a bit.
Description:
bit and sbit access the bit-array element specified by subscripts.
These functions ignore the fill pointer when accessing elements.
Examples:
(bit (setq ba (make-array 8
:element-type ’bit
:initial-element 1))
\3) → 1
(setf (bit ba 3) 0) → 0
(bit ba 3) → 0
(sbit ba 5) → 1
(setf (sbit ba 5) 1) → 1
(sbit ba 5) → 1
See Also:
aref, Section 3.2.1 (Compiler Terminology)
Notes:
bit and sbit are like aref except that they require arrays to be a bit array and a simple bit array, respectively.
bit and sbit, unlike char and schar, allow the first argument to be an array of any rank.
bit-and, bit-andc1, bit-andc2, bit-eqv, bit-ior, bit nand, bit-nor, bit-not, bit-orc1, bit-orc2, bit-xor FunctionSyntax:
bit-and bit-array1 bit-array2 &optional opt-arg → resulting-bit-array
bit-andc1 bit-array1 bit-array2 &optional opt-arg → resulting-bit-array
bit-andc2 bit-array1 bit-array2 &optional opt-arg → resulting-bit-array
bit-eqv bit-array1 bit-array2 &optional opt-arg → resulting-bit-array
bit-ior bit-array1 bit-array2 &optional opt-arg → resulting-bit-array
bit-nand bit-array1 bit-array2 &optional opt-arg → resulting-bit-array
bit-nor bit-array1 bit-array2 &optional opt-arg → resulting-bit-array
bit-orc1 bit-array1 bit-array2 &optional opt-arg → resulting-bit-array
bit-orc2 bit-array1 bit-array2 &optional opt-arg → resulting-bit-array
bit-xor bit-array1 bit-array2 &optional opt-arg → resulting-bit-array
bit-not bit-array &optional opt-arg → resulting-bit-array
Arguments and Values:
bit-array, bit-array1, bit-array2—a bit array.
Opt-arg—a bit array, or t, or nil. The default is nil.
Bit-array, bit-array1, bit-array2, and opt-arg (if an array) must all be of the same rank and dimensions.
resulting-bit-array—a bit array.
Description:
These functions perform bit-wise logical operations on bit-array1 and bit-array2 and return an array of matching rank and dimensions, such that any given bit of the result is produced by operating on corresponding bits from each of the arguments.
In the case of bit-not, an array of rank and dimensions matching bit-array is returned that contains a copy of bit-array with all the bits inverted.
If opt-arg is of type (array bit) the contents of the result are destructively placed into opt-arg. If opt-arg is the symbol t, bit-array or bit-array1 is replaced with the result; if opt-arg is nil or omitted, a new array is created to contain the result.
Figure 15–4 indicates the logical operation performed by each of the functions.
|Function Operation|
| :- |
|
bit-and and
bit-eqv equivalence (exclusive nor)
bit-not complement
bit-ior inclusive or
bit-xor exclusive or
bit-nand complement of bit-array1 and bit-array2
bit-nor complement of bit-array1 or bit-array2
bit-andc1 and complement of bit-array1 with bit-array2
bit-andc2 and bit-array1 with complement of bit-array2
bit-orc1 or complement of bit-array1 with bit-array2
bit-orc2 or bit-array1 with complement of bit-array2
|Figure 15–4. Bit-wise Logical Operations on Bit Arrays
Examples:
(bit-and (setq ba #\*11101010) #\*01101011) → #\*01101010
(bit-and #\*1100 #\*1010) → #\*1000
(bit-andc1 #\*1100 #\*1010) → #\*0010
(setq rba (bit-andc2 ba #\*00110011 t)) → #\*11001000
(eq rba ba) → true
(bit-not (setq ba #\*11101010)) → #\*00010101
(setq rba (bit-not ba
(setq tba (make-array 8
:element-type ’bit))))
→ #\*00010101
(equal rba tba) → true
(bit-xor #\*1100 #\*1010) → #\*0110
See Also:
lognot, logand
Expanded Reference: bit, sbit
Basic Bit Access
bit accesses elements of a bit array. sbit is the same but requires a simple bit array, potentially allowing faster access.
(let ((bv (make-array 8 :element-type 'bit
:initial-contents '(1 0 1 1 0 0 1 0))))
(list (bit bv 0) (bit bv 1) (bit bv 2)))
=> (1 0 1)
;; sbit works the same way on simple bit arrays
(let ((bv #*10110010))
(list (sbit bv 0) (sbit bv 3) (sbit bv 7)))
=> (1 1 0)
Setting Bits with SETF
Both bit and sbit work with setf.
(let ((bv (make-array 5 :element-type 'bit :initial-element 0)))
(setf (bit bv 1) 1)
(setf (bit bv 3) 1)
bv)
=> #*01010
Multi-Dimensional Bit Arrays
bit and sbit work on bit arrays of any rank, not just bit vectors.
(let ((ba (make-array '(2 3) :element-type 'bit
:initial-contents '((1 0 1) (0 1 0)))))
(list (bit ba 0 0) (bit ba 0 2) (bit ba 1 1)))
=> (1 1 1)
Bitwise Logical Operations on Bit Arrays
The bit-and, bit-ior, bit-xor, and bit-not family of functions perform element-wise logical operations on entire bit arrays.
(bit-and #*1100 #*1010)
=> #*1000
(bit-ior #*1100 #*1010)
=> #*1110
(bit-xor #*1100 #*1010)
=> #*0110
(bit-not #*10110)
=> #*01001
Destructive Bit Operations with opt-arg
The optional third argument controls where the result is stored. Pass t to modify the first argument in place, or pass another bit array to store the result there.
;; Store result back into the first argument
(let ((a (copy-seq #*1100))
(b #*1010))
(bit-and a b t) ; result stored in a
a)
=> #*1000
;; Create a fresh result (default, opt-arg = nil)
(let ((result (bit-ior #*1100 #*1010)))
result)
=> #*1110