* Bit shift oddity?
@ 2020-10-19 0:19 Douglas Lewan
2020-10-19 7:06 ` tomas
2020-10-19 9:23 ` Gregory Heytings via Users list for the GNU Emacs text editor
0 siblings, 2 replies; 8+ messages in thread
From: Douglas Lewan @ 2020-10-19 0:19 UTC (permalink / raw)
To: help-gnu-emacs
I see the following:
ELISP> (= (lsh -1 (lognot 0)) (lsh -2 (lognot 0)))
t
ELISP> (= (lsh -1 (lognot 0)) (lsh -3 (lognot 0)))
nil
The first seems odd to me. Is it really what's expected?
--
,Doug
d.lewan2000@gmail.com
(908) 720 7908
You know, it's amazing how much closer to 0 that 8 067 332 is than 15 is. (2020 Oct 18)
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Bit shift oddity?
2020-10-19 0:19 Bit shift oddity? Douglas Lewan
@ 2020-10-19 7:06 ` tomas
2020-10-19 7:48 ` Douglas Lewan
2020-10-19 9:23 ` Gregory Heytings via Users list for the GNU Emacs text editor
1 sibling, 1 reply; 8+ messages in thread
From: tomas @ 2020-10-19 7:06 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 545 bytes --]
On Sun, Oct 18, 2020 at 08:19:24PM -0400, Douglas Lewan wrote:
> I see the following:
>
> ELISP> (= (lsh -1 (lognot 0)) (lsh -2 (lognot 0)))
>
> t
>
> ELISP> (= (lsh -1 (lognot 0)) (lsh -3 (lognot 0)))
>
> nil
>
> The first seems odd to me. Is it really what's expected?
The manual for lsh talks about "quirky behaviour" of lsh when
both arguments are negative, in the name of backward-compatibility
and suggests resorting to ash. Cf. the Emacs lisp manual "3.8
Bitwise Operations on Integers"
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Bit shift oddity?
2020-10-19 7:06 ` tomas
@ 2020-10-19 7:48 ` Douglas Lewan
2020-10-19 9:01 ` tomas
0 siblings, 1 reply; 8+ messages in thread
From: Douglas Lewan @ 2020-10-19 7:48 UTC (permalink / raw)
To: help-gnu-emacs
Thanks. I see the same behavior with (ash).
On 10/19/20 3:06 AM, tomas@tuxteam.de wrote:
> On Sun, Oct 18, 2020 at 08:19:24PM -0400, Douglas Lewan wrote:
>> I see the following:
>>
>> ELISP> (= (lsh -1 (lognot 0)) (lsh -2 (lognot 0)))
>>
>> t
>>
>> ELISP> (= (lsh -1 (lognot 0)) (lsh -3 (lognot 0)))
>>
>> nil
>>
>> The first seems odd to me. Is it really what's expected?
> The manual for lsh talks about "quirky behaviour" of lsh when
> both arguments are negative, in the name of backward-compatibility
> and suggests resorting to ash. Cf. the Emacs lisp manual "3.8
> Bitwise Operations on Integers"
>
> Cheers
> - t
--
,Doug
d.lewan2000@gmail.com
(908) 720 7908
You know, it's amazing how much closer to 0 that 8 067 332 is than 15 is. (2020 Oct 18)
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Bit shift oddity?
2020-10-19 7:48 ` Douglas Lewan
@ 2020-10-19 9:01 ` tomas
2020-10-19 15:14 ` Stefan Monnier
0 siblings, 1 reply; 8+ messages in thread
From: tomas @ 2020-10-19 9:01 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 700 bytes --]
On Mon, Oct 19, 2020 at 03:48:01AM -0400, Douglas Lewan wrote:
> Thanks. I see the same behavior with (ash).
Well, it's not exactly the "same behaviour", the inequalities
seem to be the same, but the intermediate numbers are not.
FWIW:
(dotimes (i 10)
(let ((shift (- -1 i)))
(insert (format "shift=%d res=%d\n"
shift
(ash shift (lognot 0))))))
=>
shift=-1 res=-1
shift=-2 res=-1
shift=-3 res=-2
shift=-4 res=-2
shift=-5 res=-3
shift=-6 res=-3
shift=-7 res=-4
shift=-8 res=-4
shift=-9 res=-5
shift=-10 res=-5
I don't quite understand the result; I may look into it later.
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Bit shift oddity?
2020-10-19 9:01 ` tomas
@ 2020-10-19 15:14 ` Stefan Monnier
2020-10-19 15:30 ` tomas
0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2020-10-19 15:14 UTC (permalink / raw)
To: help-gnu-emacs
> (ash shift (lognot 0))))))
Could there be a simple misunderstanding: the shift amount is the
*second* argument. Here you always pass (lognot 0), which is -1, so
it's a right shift by 1, IOW a kind of "divide by 2".
> shift=-1 res=-1
> shift=-2 res=-1
> shift=-3 res=-2
> shift=-4 res=-2
> shift=-5 res=-3
> shift=-6 res=-3
> shift=-7 res=-4
> shift=-8 res=-4
> shift=-9 res=-5
> shift=-10 res=-5
That does indeed look like divide by 2 to me (rounding toward -∞).
Stefan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Bit shift oddity?
2020-10-19 0:19 Bit shift oddity? Douglas Lewan
2020-10-19 7:06 ` tomas
@ 2020-10-19 9:23 ` Gregory Heytings via Users list for the GNU Emacs text editor
2020-10-19 15:19 ` Douglas Lewan
1 sibling, 1 reply; 8+ messages in thread
From: Gregory Heytings via Users list for the GNU Emacs text editor @ 2020-10-19 9:23 UTC (permalink / raw)
To: Douglas Lewan; +Cc: help-gnu-emacs
>
> I see the following:
>
> ELISP> (= (lsh -1 (lognot 0)) (lsh -2 (lognot 0)))
>
> t
>
> ELISP> (= (lsh -1 (lognot 0)) (lsh -3 (lognot 0)))
>
> nil
>
> The first seems odd to me. Is it really what's expected?
>
Yes, it's what is expected. The docstring of lsh says:
"(lsh VALUE COUNT) ... If COUNT is negative, shifting is actually to the
right. In this case, if VALUE is a negative fixnum treat it as unsigned
..."
-1 (decimal) = 111...111 (binary)
-2 (decimal) = 111...110 (binary)
If you shift these two numbers to the right, you get the exact same
result: 011...111, that is, `most-positive-fixnum'.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Bit shift oddity?
2020-10-19 9:23 ` Gregory Heytings via Users list for the GNU Emacs text editor
@ 2020-10-19 15:19 ` Douglas Lewan
0 siblings, 0 replies; 8+ messages in thread
From: Douglas Lewan @ 2020-10-19 15:19 UTC (permalink / raw)
To: Gregory Heytings; +Cc: help-gnu-emacs
Nice. Thank you.
On 10/19/20 5:23 AM, Gregory Heytings wrote:
>
>>
>> I see the following:
>>
>> ELISP> (= (lsh -1 (lognot 0)) (lsh -2 (lognot 0)))
>>
>> t
>>
>> ELISP> (= (lsh -1 (lognot 0)) (lsh -3 (lognot 0)))
>>
>> nil
>>
>> The first seems odd to me. Is it really what's expected?
>>
>
> Yes, it's what is expected. The docstring of lsh says:
>
> "(lsh VALUE COUNT) ... If COUNT is negative, shifting is actually to
> the right. In this case, if VALUE is a negative fixnum treat it as
> unsigned ..."
>
> -1 (decimal) = 111...111 (binary)
> -2 (decimal) = 111...110 (binary)
>
> If you shift these two numbers to the right, you get the exact same
> result: 011...111, that is, `most-positive-fixnum'.
--
,Doug
d.lewan2000@gmail.com
(908) 720 7908
You know, it's amazing how much closer to 0 that 8 067 332 is than 15 is. (2020 Oct 18)
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2020-10-19 15:30 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-19 0:19 Bit shift oddity? Douglas Lewan
2020-10-19 7:06 ` tomas
2020-10-19 7:48 ` Douglas Lewan
2020-10-19 9:01 ` tomas
2020-10-19 15:14 ` Stefan Monnier
2020-10-19 15:30 ` tomas
2020-10-19 9:23 ` Gregory Heytings via Users list for the GNU Emacs text editor
2020-10-19 15:19 ` Douglas Lewan
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.