在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
bit = {data32={}} for i = 1, 32 do bit.data32[i] = 2^(32-i) end function bit:d2b( arg ) local num = tonumber( arg ) local tr = {} if num ~= nil then print("num = "..num) if num >= 0 then for i = 1, 32 do if num >= bit.data32[i] then num = num - bit.data32[i] tr[i] = 1 else tr[i] = 0 end end return tr else return tr end else return tr end end function judge0or1( arg ) if arg == nil then return 0 end for i = 1, #arg do if arg[i] ~= 0 or arg[i] ~= 1 then return 0 end end return 1 end function bit:b2d( arg ) local num = 0 if arg ~= nil then for i = 1, 32 do if arg[i] == 1 then num = num + bit.data32[i] elseif arg[i] ~= 0 then return num end end return num else return num end end function bit:prit( arg ) local tr = bit:d2b(arg) if tr == nil then print("o") return end for i = 1, #tr do print(tr[i]) end end function bit:lsh( arg , n ) local num = tonumber( arg ) local n1 = tonumber( n ) local rr = bit:d2b(0) local tr = {} if num == nil then return 0 end if n1 == nil then return 0 end local num1 = bit:d2b ( arg ) if n1 < 32 and n1 > 0 then for i = 1, 32 - n1 do num1[i] = num1[i+n1] num1[i+n1] = 0 end rr = num1 end return bit:b2d(rr) end function bit:rsh( arg , n ) local num = tonumber( arg ) local n1 = tonumber( n ) local rr = bit:d2b(0) local tr = {} if num == nil then return 0 end if num < 0 then return 0 end if n1 == nil then return 0 end local num1 = bit:d2b ( arg ) if n1 < 32 and n1 > 0 then for i = 32 - n1, 1, -1 do num1[i+n1] = num1[i] num1[i] = 0 end rr = num1 end return bit:b2d(rr) end function bit:aand( arg1 , arg2 ) local num1 = tonumber( arg1 ) local num2 = tonumber( arg2 ) if num1 == nil then return 0 end if num2 == nil then return 0 end local tr1 = bit:d2b ( num1 ) local tr2 = bit:d2b ( num2 ) local tr3 ={} for i = 1, 32 do if tr1[i] == 1 and tr2[i] == 1 then tr3[i] = 1 else tr3[i] = 0 end end return bit:b2d(tr3) end function bit:oor( arg1 , arg2 ) local num1 = tonumber( arg1 ) local num2 = tonumber( arg2 ) if num1 == nil then return 0 end if num2 == nil then return 0 end local tr1 = bit:d2b ( num1 ) local tr2 = bit:d2b ( num2 ) local tr3 ={} for i = 1, 32 do if tr1[i] == 0 and tr2[i] == 0 then tr3[i] = 0 else tr3[i] = 1 end end return bit:b2d(tr3) end
|
请发表评论