Skip to main content

ldb-test

ldb-test Function

Syntax:

ldb-test bytespec integer ! generalized-boolean

Arguments and Values:

bytespec—a byte specifier .

integer—an integer .

generalized-boolean—a generalized boolean.

Description:

Returns true if any of the bits of the byte in integer specified by bytespec is non-zero; otherwise returns false.

Examples:

(ldb-test (byte 4 1) 16) *! true* 
(ldb-test (byte 3 1) 16) *! false*
(ldb-test (byte 3 2) 16) *! true*

See Also:

byte, ldb, zerop

Notes:

(ldb-test bytespec n)

(not (zerop (ldb bytespec n)))

(logtest (ldb bytespec -1) n)

Expanded Reference: ldb-test

Basic usage

ldb-test returns true if any bit in the specified byte of the integer is non-zero.

(ldb-test (byte 4 1) 16)
=> T
(ldb-test (byte 3 1) 16)
=> NIL
(ldb-test (byte 3 2) 16)
=> T

Testing if a field is zero

ldb-test is a convenient way to check if a particular bit field has any bits set.

(ldb-test (byte 4 0) 0)
=> NIL
(ldb-test (byte 4 0) #xF0)
=> NIL
(ldb-test (byte 4 4) #xF0)
=> T

Testing individual bits

With a byte of size 1, ldb-test works like logbitp.

(ldb-test (byte 1 0) 5)
=> T
(ldb-test (byte 1 1) 5)
=> NIL
(ldb-test (byte 1 2) 5)
=> T

Equivalence to checking ldb against zero

ldb-test is equivalent to (not (zerop (ldb bytespec n))).

(let ((bs (byte 4 4))
(n #xAB))
(eql (ldb-test bs n)
(not (zerop (ldb bs n)))))
=> T

Practical use: checking if a flag group is active

;; Check if any of the permission bits (bits 0-2) are set
(let ((permissions #b101))
(ldb-test (byte 3 0) permissions))
=> T

;; Check if any high bits (bits 8-15) are set in a small number
(ldb-test (byte 8 8) 255)
=> NIL