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