Lua 5.1 位操作(与,或,异或操作)

由于lua5.1不支持位操作,自己写了一个lua的位操作函数,代码如下:

方法1:

复制代码
function Xor(num1,num2)
    local tmp1 = num1
    local tmp2 = num2
    local str = ""
    repeat
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 then
            str = "0"..str
        else
            str = "1"..str
        end
        tmp1 = math.modf(tmp1/2)
        tmp2 = math.modf(tmp2/2)
    until(tmp1 == 0 and tmp2 == 0)
    return tonumber(str,2)
end
 
function And(num1,num2)
    local tmp1 = num1
    local tmp2 = num2
    local str = ""
    repeat
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 then
            if s1 == 1 then
                str = "1"..str
            else
                str = "0"..str
            end
        else
            str = "0"..str
        end
        tmp1 = math.modf(tmp1/2)
        tmp2 = math.modf(tmp2/2)
    until(tmp1 == 0 and tmp2 == 0)
    return tonumber(str,2)
end
 
function Or(num1,num2)
    local tmp1 = num1
    local tmp2 = num2
    local str = ""
    repeat 
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 then
            if s1 == 0 then
                str = "0"..str
            else
                str = "1"..str
            end
        else
            str = "1"..str
        end
        tmp1 = math.modf(tmp1/2)
        tmp2 = math.modf(tmp2/2)
    until(tmp1 == 0 and tmp2 == 0)
    return tonumber(str,2)
end
复制代码

使用方法如下:

local tmp1 = 0x52
local tmp2 = 0x01
 
print(Xor(tmp1,tmp2))  --输出tmp1 异或 tmp2 的操作结果
print(And(tmp1,tmp2))    --输出tmp1 与 tmp2 的操作结果
print(Or(tmp1,tmp2))    --输出tmp1 或 tmp2 的操作结果

 

加入支持负数的位运算操作: 但是负数和负数位操作出来的显示是个整数

如 And(-8,-5)    结果 :4294967288  在32位机器上就是 -8

复制代码
function checkNums( nums )
    local n = nums
    if n >= 0 then
        return n
    else
        n = 0 - n
        n = 0xffffffff - n + 1
    end
    return n
end
function resultCover( n )
    local  num = n
    if num >= 0x80000000 then
        num = num - 0xffffffff - 1
    end
    return num
end
 
function And(num1,num2)
    local tmp1 = checkNums(num1)
    local tmp2 = checkNums(num2)
    local ret = 0
    local count = 0
    repeat
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 and s1 == 1 then
            ret = ret + 2^count
        end
        tmp1 = math.modf(tmp1/2)
        tmp2 = math.modf(tmp2/2)
        count = count + 1
    until(tmp1 == 0 and tmp2 == 0)
    return resultCover(ret)
end
 
function Or(num1,num2)
    local tmp1 = checkNums(num1)
    local tmp2 = checkNums(num2)
    local ret = 0
    local count = 0
    repeat
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 and s1 == 0 then
 
        else
            ret = ret + 2^count
        end
        tmp1 = math.modf(tmp1/2)
        tmp2 = math.modf(tmp2/2)
        count = count + 1
    until(tmp1 == 0 and tmp2 == 0)
    return resultCover(ret)
end
 
 
function Xor(num1,num2)
    local tmp1 = checkNums(num1)
    local tmp2 = checkNums(num2)
    local ret = 0
    local count = 0
    repeat
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 ~= s2 then
            ret = ret + 2^count
        end
        tmp1 = math.modf(tmp1/2)
        tmp2 = math.modf(tmp2/2)
        count = count + 1
    until(tmp1 == 0 and tmp2 == 0)
    return resultCover(ret)
end
复制代码

5.3开始自带位操作:

复制代码
&    按位与
|    按位或
~    按位异或
>>    右移
<<    左移
~    按位非
使用方法:
c = a & b      
c = a | b
c = a ~ b
c = a >> b
c = a << b
c = ~a
复制代码

https://blog.csdn.net/u013625451/article/details/84644839

posted @   笠航  阅读(5004)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示