lua-users home
lua-l archive

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


local bit32={}
bit32.extract=function(x,s,n) n=n or 1 return (x>>s)&((1<<n)-1) end
bit32.lshift=function(x,n) return x<<n end
bit32.bxor=function(a,b) return a~b end

чт, 24 окт. 2019 г. в 07:31, Jonathan Goble <jcgoble3@gmail.com>:
>
> On Wed, Oct 23, 2019 at 11:44 PM Russell Haley <russ.haley@gmail.com> wrote:
> >
> > Hi,
> >
> > Can anybody help me convert this 5.2 code to 5.3? I suck at binary stuff.
> >
> > My attempts to replicate bit32.extract yielded little worth posting:
>
> It may help to look at the source code for bit32.extract:
> https://www.lua.org/source/5.2/lbitlib.c.html#b_extract
>
> The function is quite short, and the actual work with bitwise
> operations is a one-liner (if you consider the "mask" macro to be part
> of that one-liner). It essentially right-shifts it by an amount equal
> to the desired low bit, then ANDs it with a mask equal in width to the
> desired field size (which in your case is just 1).
>