• the unsigned right shift operator in CL >>>

    From Madhu@enometh@meer.net to comp.lang.lisp on Wed Oct 15 12:34:22 2025
    From Newsgroup: comp.lang.lisp


    I was trying to wrap my head the >>> operator

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unsigned_right_shift

    The unsigned right shift (>>>) operator returns a number whose
    binary representation is the first operand shifted by the
    specified number of bits to the right. Excess bits shifted off
    to the right are discarded, and zero bits are shifted in from
    the left. This operation is also called "zero-filling right
    shift", because the sign bit becomes 0, so the resulting number
    is always positive.

    This is what I understood but it looks too complicated

    (defun signed->unsigned (num bits)
    (if (< (- (1+ (expt 2 (1- bits)))) num 0)
    (+ (expt 2 bits) num)
    (if (< num (expt 2 bits))
    num
    (logand (1- (expt 2 bits)) num))))

    (defun fixed-width-ash (width x n)
    (if (> x 0)
    (logand (1- (ash 1 width)) (ash x n))
    (fixed-width-ash width
    (signed->unsigned x 32) n)))

    -9 >>> 2
    1073741821

    (fixed-width-ash 32 -9 -2)
    1073741821

    I'm looking for any insights on this.

    PS. Anyone know what happened to PJB, his online presence seems to have
    been discontinues.

    https://stackoverflow.com/questions/1691292/how-to-do-bit-wise-zero-filling-right-shift-in-scheme
    https://stackoverflow.com/questions/26151644/why-is-there-no-unsigned-left-shift-operator-in-java
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Aidan Kehoe@kehoea@parhasard.net to comp.lang.lisp on Thu Oct 16 19:04:13 2025
    From Newsgroup: comp.lang.lisp


    Ar an c||igi|| l|i d|-ag de m|! Deireadh F||mhair, scr|!obh Madhu:

    I was trying to wrap my head the >>> operator

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unsigned_right_shift

    The unsigned right shift (>>>) operator returns a number whose
    binary representation is the first operand shifted by the
    specified number of bits to the right. Excess bits shifted off
    to the right are discarded, and zero bits are shifted in from
    the left. This operation is also called "zero-filling right
    shift", because the sign bit becomes 0, so the resulting number
    is always positive.

    This is what I understood but it looks too complicated

    Something like:

    (defun fixed-width-ash (width value count)
    (ash (logand value (lognot (ash -1 width))) count))

    Construct an integer of the desired width that is all 1s, logand it with VALUE, then shift by COUNT.

    PS. Anyone know what happened to PJB, his online presence seems to have
    been discontinues.

    I hadnrCOt followed him, worth reading?

    https://stackoverflow.com/questions/1691292/how-to-do-bit-wise-zero-filling-right-shift-in-scheme
    https://stackoverflow.com/questions/26151644/why-is-there-no-unsigned-left-shift-operator-in-java
    --
    rCyAs I sat looking up at the Guinness ad, I could never figure out /
    How your man stayed up on the surfboard after fourteen pints of stoutrCO
    (C. Moore)
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Alan Bawden@alan@csail.mit.edu to comp.lang.lisp on Thu Oct 16 16:06:39 2025
    From Newsgroup: comp.lang.lisp

    Aidan Kehoe <kehoea@parhasard.net> writes:

    Ar an c||igi|| l|i d|-ag de m|! Deireadh F||mhair, scr|!obh Madhu:

    Something like:

    (defun fixed-width-ash (width value count)
    (ash (logand value (lognot (ash -1 width))) count))

    Construct an integer of the desired width that is all 1s, logand it
    with VALUE, then shift by COUNT.

    Your compiler will probably recognize that this is equivalent to:

    (defun fixed-width-ash (width value count)
    (ash (logandc2 value (ash -1 width)) count))

    But I would write it that way in the first place because I like to
    remember that the PDP-10 had an ANDCM instruction...
    --
    Alan Bawden
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Aidan Kehoe@kehoea@parhasard.net to comp.lang.lisp on Thu Oct 16 21:55:26 2025
    From Newsgroup: comp.lang.lisp


    Ar an s|-|| l|i d|-ag de m|! Deireadh F||mhair, scr|!obh Alan Bawden:

    Aidan Kehoe <kehoea@parhasard.net> writes:

    Ar an c||igi|| l|i d|-ag de m|! Deireadh F||mhair, scr|!obh Madhu:

    Something like:

    (defun fixed-width-ash (width value count)
    (ash (logand value (lognot (ash -1 width))) count))

    To comment on my own code; this wonrCOt do the desired fixed-width treatment of COUNT is positive, the logand would need to be done on the result of the #'ash. But the discussion was about implementing >>>, where COUNT is negative.

    Construct an integer of the desired width that is all 1s, logand it
    with VALUE, then shift by COUNT.

    Your compiler will probably recognize that this is equivalent to:

    (defun fixed-width-ash (width value count)
    (ash (logandc2 value (ash -1 width)) count))

    But I would write it that way in the first place because I like to
    remember that the PDP-10 had an ANDCM instruction...

    I donrCOt expect to remember any other PDP-10 instructions, but I might remember
    this one. Thanks!
    --
    rCyAs I sat looking up at the Guinness ad, I could never figure out /
    How your man stayed up on the surfboard after fourteen pints of stoutrCO
    (C. Moore)
    --- Synchronet 3.21a-Linux NewsLink 1.2