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
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
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:
Aidan Kehoe <kehoea@parhasard.net> writes:
Something like:
(defun fixed-width-ash (width value count)
(ash (logand value (lognot (ash -1 width))) count))
To comment on my own code; this won't 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))
--- Synchronet 3.21a-Linux NewsLink 1.2But I would write it that way in the first place because I like to remember that the PDP-10 had an ANDCM instruction...
I don't expect to remember any other PDP-10 instructions, but I might remember
this one. Thanks!
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)^^^^ 32 here should be `width'
(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
PS. Anyone know what happened to PJB, his online presence seems to have
been discontinues(d)
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...
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 54 |
| Nodes: | 6 (1 / 5) |
| Uptime: | 23:40:59 |
| Calls: | 742 |
| Files: | 1,218 |
| D/L today: |
6 files (8,794K bytes) |
| Messages: | 186,855 |
| Posted today: | 1 |