turtle/bit32.lua

99 lines
1.8 KiB
Lua
Raw Normal View History

2013-11-30 19:43:31 +01:00
bit32 = {}
local N = 32
local P = 2^N
2015-01-25 10:31:23 +01:00
function bit32.bnot(x)
x = x % P
return P - 1 - x
2013-11-30 19:43:31 +01:00
end
2015-01-25 10:31:23 +01:00
function bit32.band(x, y)
2013-11-30 19:43:31 +01:00
-- Common usecases, they deserve to be optimized
2015-01-25 10:31:23 +01:00
if y == 0xff then return x % 0x100 end
if y == 0xffff then return x % 0x10000 end
if y == 0xffffffff then return x % 0x100000000 end
2013-11-30 19:43:31 +01:00
2015-01-25 10:31:23 +01:00
x, y = x % P, y % P
2013-11-30 19:43:31 +01:00
local r = 0
local p = 1
for i = 1, N do
2015-01-25 10:31:23 +01:00
local a, b = x % 2, y % 2
x, y = math.floor(x / 2), math.floor(y / 2)
if a + b == 2 then
2013-11-30 19:43:31 +01:00
r = r + p
end
2015-01-25 10:31:23 +01:00
p = 2 * p
2013-11-30 19:43:31 +01:00
end
return r
end
2015-01-25 10:31:23 +01:00
function bit32.bor(x, y)
2013-11-30 19:43:31 +01:00
-- Common usecases, they deserve to be optimized
2015-01-03 14:11:04 +01:00
if y == 0xff then return x - (x%0x100) + 0xff end
if y == 0xffff then return x - (x%0x10000) + 0xffff end
if y == 0xffffffff then return 0xffffffff end
2013-11-30 19:43:31 +01:00
2015-01-25 10:31:23 +01:00
x, y = x % P, y % P
2013-11-30 19:43:31 +01:00
local r = 0
local p = 1
for i = 1, N do
2015-01-25 10:31:23 +01:00
local a, b = x % 2, y % 2
x, y = math.floor(x / 2), math.floor(y / 2)
if a + b >= 1 then
2013-11-30 19:43:31 +01:00
r = r + p
end
2015-01-25 10:31:23 +01:00
p = 2 * p
2013-11-30 19:43:31 +01:00
end
return r
end
2015-01-25 10:31:23 +01:00
function bit32.bxor(x, y)
x, y = x % P, y % P
2013-11-30 19:43:31 +01:00
local r = 0
local p = 1
for i = 1, N do
2015-01-25 10:31:23 +01:00
local a, b = x%2, y%2
2013-11-30 19:43:31 +01:00
x, y = math.floor(x/2), math.floor(y/2)
2015-01-25 10:31:23 +01:00
if a + b == 1 then
2013-11-30 19:43:31 +01:00
r = r + p
end
2015-01-25 10:31:23 +01:00
p = 2 * p
2013-11-30 19:43:31 +01:00
end
return r
end
2015-01-25 10:31:23 +01:00
function bit32.lshift(x, s_amount)
2013-11-30 19:43:31 +01:00
if math.abs(s_amount) >= N then return 0 end
2015-01-25 10:31:23 +01:00
x = x % P
2013-11-30 19:43:31 +01:00
if s_amount < 0 then
2015-01-25 10:31:23 +01:00
return math.floor(x * (2 ^ s_amount))
2013-11-30 19:43:31 +01:00
else
2015-01-25 10:31:23 +01:00
return (x * (2 ^ s_amount)) % P
2013-11-30 19:43:31 +01:00
end
end
2015-01-25 10:31:23 +01:00
function bit32.rshift(x, s_amount)
2013-11-30 19:43:31 +01:00
if math.abs(s_amount) >= N then return 0 end
2015-01-25 10:31:23 +01:00
x = x % P
2013-11-30 19:43:31 +01:00
if s_amount > 0 then
2015-01-25 10:31:23 +01:00
return math.floor(x * (2 ^ - s_amount))
2013-11-30 19:43:31 +01:00
else
2015-01-25 10:31:23 +01:00
return (x * (2 ^ -s_amount)) % P
2013-11-30 19:43:31 +01:00
end
end
2015-01-25 10:31:23 +01:00
function bit32.arshift(x, s_amount)
2013-11-30 19:43:31 +01:00
if math.abs(s_amount) >= N then return 0 end
2015-01-25 10:31:23 +01:00
x = x % P
2013-11-30 19:43:31 +01:00
if s_amount > 0 then
local add = 0
if x >= P/2 then
2015-01-25 10:31:23 +01:00
add = P - 2 ^ (N - s_amount)
2013-11-30 19:43:31 +01:00
end
2015-01-25 10:31:23 +01:00
return math.floor(x * (2 ^ -s_amount)) + add
2013-11-30 19:43:31 +01:00
else
2015-01-25 10:31:23 +01:00
return (x * (2 ^ -s_amount)) % P
2013-11-30 19:43:31 +01:00
end
end