• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

lua位运算

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
解释型语言和编译型语言如何交互?以lua和c为例发布时间:2022-07-22
下一篇:
lua -- encode and decode发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap