Lua bit arithmetic

Source: Internet
Author: User

bit = {data32={}} fori =1, +  DoBit.data32[i]=2^( +-i)Endfunctionbit:d2b (ARG)Localnum =Tonumber(ARG)LocalTR = {}        ifNum ~=Nil  Then        Print("num =".. NUM)ifNum >=0  Then             fori =1, +  Do                 ifNum >= Bit.data32[i] ThenNum= num-Bit.data32[i] Tr[i]=1                ElseTr[i]=0                End            End            returnTRElse            returnTREnd                                    Else         returnTREnd        EndfunctionJudge0or1 (ARG)ifarg = =Nil  Then        return 0    End         fori =1, #arg Do        ifArg[i] ~=0 orArg[i] ~=1  Then            return 0        End    End        return 1Endfunctionbit:b2d (ARG)Localnum =0        ifArg ~=Nil  Then         fori =1, +  Do            ifArg[i] = =1     ThenNum= num +Bit.data32[i]ElseIfArg[i] ~=0  Then                returnNumEnd                        End             returnNumElse       returnNumEnd    EndfunctionBit:prit (ARG)LocalTR =bit:d2b (ARG)ifTR = =Nil  Then        Print("o")        return    End     fori =1, #tr Do        Print(Tr[i])EndEndfunctionBit:lsh (ARG, N)Localnum =Tonumber(ARG)LocalN1 =Tonumber(n)Localrr = BIT:D2B (0)    LocalTR = {}        ifnum = =Nil  Then        return 0    End        ifN1 = =Nil  Then        return 0    End        LocalNUM1 =bit:d2b (ARG)ifN1 < +  andN1 >0  Then         fori =1, +-N1 DoNum1[i]= num1[i+N1] Num1[i+N1] =0                    EndRR=NUM1End        returnbit:b2d (RR)EndfunctionBit:rsh (ARG, N)Localnum =Tonumber(ARG)LocalN1 =Tonumber(n)Localrr = BIT:D2B (0)    LocalTR = {}        ifnum = =Nil  Then        return 0    End        ifNum <0  Then        return 0    End        ifN1 = =Nil  Then        return 0    End        LocalNUM1 =bit:d2b (ARG)ifN1 < +  andN1 >0  Then         fori = +-N1,1, -1  DoNum1[i+N1] =Num1[i] Num1[i]=0                    EndRR=NUM1End        returnbit:b2d (RR)EndfunctionBit:aand (arg1, arg2)LocalNUM1 =Tonumber(arg1)Localnum2 =Tonumber(ARG2)ifNUM1 = =Nil  Then        return 0    End        ifNum2 = =Nil  Then        return 0    End        LocalTR1 =bit:d2b (NUM1)LocalTR2 =bit:d2b (num2)LocalTR3 ={}         fori =1, +  Do       ifTr1[i] = =1  andTr2[i] = =1  ThenTr3[i]=1       ElseTr3[i]=0       End    End         returnbit:b2d (TR3)EndfunctionBit:oor (arg1, arg2)LocalNUM1 =Tonumber(arg1)Localnum2 =Tonumber(ARG2)ifNUM1 = =Nil  Then        return 0    End        ifNum2 = =Nil  Then        return 0    End        LocalTR1 =bit:d2b (NUM1)LocalTR2 =bit:d2b (num2)LocalTR3 ={}         fori =1, +  Do       ifTr1[i] = =0  andTr2[i] = =0  ThenTr3[i]=0       ElseTr3[i]=1       End    End         returnbit:b2d (TR3)End

Lua bit arithmetic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.