lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Mar 25, 2014 at 12:29 PM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
> Correcting some details: I guess "the former requires a single extra
> operation" only applies for constant offsets, or am I missing something?
> (For an unknown offset, I could not find a way to synthesize a logical
> shift with less than three extra operations.)  But, for constant
> offsets, an integer division synthesizes an arithmetic shift with no
> extra operation.
>
> I still would like to hear about the usefulness of both shifts.
>
> -- Roberto
>

I was referring to constant offsets, yes.

Now, for me, when I'm doing operations where I'm doing a shift-right
and not already masking anyway, I DO use the >> operator
arithmetically. I tend to employ it as an integer exponentiation
operator -- x << k is the same as x * (2^k), and x >> k is the same as
floor(x / (2^k)), without the cost of actually performing an
exponentiation.

When I actually NEED a logical shift, I'm ALREADY masking, so the
underlying implementation tends to be irrelevant.

/s/ Adam