Basic examples:
(logext 4 7) --> 7 Bottom four bits are 0111 Sign bit is 0 Sign extension creates {0000......0}111 2's complement interpretation: 7. (logext 3 7) --> -1 Bottom 3 bits are 111 Sign bit is 1 Sign extension creates {1111.....1}111 2's complement interpretation: -1. (logext 4 8) --> -8 Bottom 4 bits are 1000 Sign bit is 1 Sign extension creates {1111.....1}1000 2's complement interpretation: -8.
This function returns a (possibly negative) integer. For consistency with
signed-byte-p,
We specify
See also bitops::bitops/fast-logext for a logically identical function that is optimized for better performance.
Function:
(defun logext (size i) (declare (type unsigned-byte size)) (declare (xargs :guard (and (and (integerp size) (< 0 size)) (integerp i)))) (declare (xargs :split-types t)) (let ((__function__ 'logext)) (declare (ignorable __function__)) (let* ((size-1 (- size 1))) (declare (type unsigned-byte size-1)) (logapp size-1 i (if (logbitp size-1 i) -1 0)))))
Theorem:
(defthm logext-type (b* ((int (logext size i))) (integerp int)) :rule-classes :type-prescription)