[pro] Looking for Sign-extend elegance

Faré fahree at gmail.com
Wed Sep 8 18:14:33 UTC 2010


On 8 September 2010 13:18, David McClain <dbm at refined-audiometrics.com> wrote:
> I have a 64-bit integer obtained by the FLI from external storage. It was
> obtained by reading two 32-bit unsigned values and depositing them into a
> zero valued integer. Now I want to sign-extend the result so that if it has
> its MSB set, the value will be a twos-complement negative value.
> (defun read-int64 (ptr)
>   (let ((v (read-uint64 ptr)))
>     (if (logbitp 63 v)
>         (- v #.(ash 1 64))
>       v)))
> But this looks inelegant to me, requiring the storage of a constant #.(ash 1
> 64) used in a subtraction operation.
> Been racking my brain on the BOOLE operations and the LOGNOT et al, looking
> for a more elegant solution to this. Not a high-value item, just curious.

(if (logbitp 63 v) (dpb v (byte 64 0) -1) v)

[ François-René ÐVB Rideau | Reflection&Cybernethics | http://fare.tunes.org ]
Nostalgia isn’t what it used to be.




More information about the pro mailing list