• Re: the unsigned right shift operator in CL >>>

    From Kaz Kylheku@046-301-5902@kylheku.com to comp.lang.lisp on Thu Mar 12 18:00:17 2026
    From Newsgroup: comp.lang.lisp

    On 2025-10-24, Alan Bawden <alan@csail.mit.edu> wrote:
    Madhu <enometh@meer.net> writes:

    something like this then, does it miss out any case?

    (defun fixed-width-ash (width value count)
    (if (< count 0)
    (ash (logand value (lognot (ash -1 width))) count)
    (ash (logandc2 value (ash -1 width)) count)))

    Uh, guys, my point was that (logandc2 x y) and (logand x (lognot y))
    compute _exactly_ the same thing...

    So I'm looking for the logtrunc function and it's not there!

    Right, it's something I added in TXR Lisp, which is not in Common Lisp.

    (defun lsh (width value count) ;; ash -> arithmetic shift; lsh -> logical
    (ash (logtrunc value width) count))

    logtrunc truncates an integer to the specified number of binary
    digits, producing a nonnegative value. Negative integers are taken to be in the familiar "infinite two's complement", where we have a mantissa followed by an infinite number of sign bits that are 1, so that (logtrunc -1 8) produces 255, etc.
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Stefan Monnier@monnier@iro.umontreal.ca to comp.lang.lisp on Thu Mar 12 16:34:28 2026
    From Newsgroup: comp.lang.lisp

    So I'm looking for the logtrunc function and it's not there!

    Do you mean something like:

    (defun logtrunc (n bits)
    (mod n (ash 1 bits)))

    aka

    (defun logtrunc (n bits)
    (logand n (1- (ash 1 bits))))

    ?


    === Stefan
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Kaz Kylheku@046-301-5902@kylheku.com to comp.lang.lisp on Mon Mar 16 20:14:57 2026
    From Newsgroup: comp.lang.lisp

    On 2026-03-12, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
    So I'm looking for the logtrunc function and it's not there!

    Do you mean something like:

    (defun logtrunc (n bits)
    (mod n (ash 1 bits)))

    aka

    (defun logtrunc (n bits)
    (logand n (1- (ash 1 bits))))


    Yes, these look equivalent to me. A compiler could recognize
    these patterns and contract to a "logtrunc" operation that
    operates on the representation to do the truncation directly.
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca
    --- Synchronet 3.21d-Linux NewsLink 1.2