develooper Front page | perl.perl5.porters | Postings from June 2015

Re: Coverity finding: shift by negative

Thread Previous | Thread Next
From:
Jan Dubois
Date:
June 23, 2015 00:58
Subject:
Re: Coverity finding: shift by negative
Message ID:
CAD-TLz8_z3b28K-f7NKN4pLKmAQQw7=uuOirKbtHr4ojjjf3WQ@mail.gmail.com
On Mon, Jun 22, 2015 at 5:03 PM, Jarkko Hietaniemi <jhi@iki.fi> wrote:
> Now, the question is, what should we do?   What should we return?
>
> (Yes, I'm asking should we define a semantics for this thing which is
> undefined in C.)
>
> undef?

Possible, but useless.  I would rather it throws an error (like
division by 0) if it doesn't produce an "expected" result.

> shift left by negative n == shift right by -n ?
> shift right by negative n == shift left by -n?

Yes, that's what I would expect a high-level language to do.

It also means that $a << $b == $a * 2**$b, even for negative $b, which
is again something I naively expect. :)

I believe I once wrote $a << ($p-$q) and expected this generalized
behaviour, but can't remember any context.

> shift by too many bits == shift by n modulo wordbits?
> (with a sane definition of modulo for negative -n...)

Surely a right shift by too many bits should result in 0.
I'm less sure about the left shift, but I think I would rather see the
far left bits dropped.

The modulo operation would turn the shift into a rotate, which should
be a separate operator. Of course for rotation using negative numbers
to change direction makes even more sense.

<just kidding>Maybe <<> for rotate left and <>> for rotate right?</j/k>

Cheers,
-Jan

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About