-数字转二进制
function ToSecond(num)
local str = ""
local tmp = num
while (tmp > 0) do
if (tmp % 2 == 1) then
str = str .. "1"
else
str = str .. "0"
end
tmp = math.modf(tmp / 2)
end
str = string.reverse(str)
return str
end
--先补齐两个数字的二进制位数
function makeSameLength(num1, num2)
local str1 = ToSecond(num1)
local str2 = ToSecond(num2)
local len1 = string.len(str1)
local len2 = string.len(str2)
print("len1 = " .. len1)
print("len2 = " .. len2)
local len = 0
local x = 0
if (len1 > len2) then
x = len1 - len2
for i = 1, x do
str2 = "0" .. str2
end
len = len1
print("len = "..len)
elseif (len2 > len1) then
x = len2 - len1
for i = 1, x do
str1 = "0" .. str1
end
len = len2
end
len = len1
return str1, str2, len
end
--按位与
function BitAnd(num1, num2)
local str1, str2, len = makeSameLength(num1, num2)
local rtmp = ""
for i = 1, len do
local st1 = tonumber(string.sub(str1, i, i))
local st2 = tonumber(string.sub(str2, i, i))
if(st1 == 0) then
rtmp = rtmp .. "0"
else
if (st2 ~= 0) then
rtmp = rtmp .. "1"
else
rtmp = rtmp .. "0"
end
end
end
rtmp = tostring(rtmp)
return rtmp
end
OpenCV——图像按位运算
2023-12-10 09:52:05 29 阅读