lua-users home
lua-l archive

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


On Sat, Apr 7, 2018 at 1:58 PM, Reinder Feenstra
<reinderfeenstra@gmail.com> wrote:
> Hi All,
>
> While implementing all meta methods in some glue code between Lua and
> C++ I'd became curious why __bshr and __bshl both exist. For the
> compare operators a smart trick is used so you don't need to implement
> them all.
>
> To make sure my glue code behaves the same as Lua I simply tried all
> operators, e.g.
>
> Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
>> print(2 << -1)
> 1
>> print(2 >> 1)
> 1
>
> This is legal in Lua, so why not only a __bshr meta method and call
> this with a negative shift count for the << operator?
>
> Just curiosity, no complain ;) I really like Lua and its easiness to
> embed. (I also experimented with python and v8)
>
> Best regards,
> Reinder
>

Not everything that uses << or >> is performing something analogous to
a symmetric bit shift.

C++ developers, for example, use it to mean insertion and extraction.

I don't know off the top of my head if there are any popular Lua
libraries that use the operators for asymmetric behaviors, but it
wouldn't surprise me if LPeg were among them. (I haven't actually used
it!)

/s/ Adam