mirror of
https://github.com/nmap/nmap.git
synced 2025-12-06 04:31:29 +00:00
94 lines
2.0 KiB
Lua
94 lines
2.0 KiB
Lua
---
|
|
-- Bitwise operations on integers.
|
|
--
|
|
-- THIS LIBRARY IS DEPRECATED, Please use native Lua 5.3 bitwise operators.
|
|
--
|
|
-- @copyright BSD License
|
|
-- @see https://www.lua.org/manual/5.3/manual.html#3.4.2
|
|
-- @class module
|
|
-- @name bit
|
|
|
|
local select = select
|
|
|
|
local _ENV = {}
|
|
|
|
--- Returns the one's complement of <code>a</code>.
|
|
--
|
|
-- REPLACEMENT: <code>~a</code>
|
|
-- @param a Number.
|
|
-- @return The one's complement of <code>a</code>.
|
|
function bnot(a)
|
|
return ~a
|
|
end
|
|
|
|
--- Returns the bitwise and of all its arguments.
|
|
--
|
|
-- REPLACEMENT: <code>a & b</code>
|
|
-- @param ... A variable number of Numbers to and.
|
|
-- @return The anded result.
|
|
function band(a, b, ...)
|
|
a = a & b
|
|
if select("#", ...) > 0 then
|
|
return band(a, ...)
|
|
else
|
|
return a
|
|
end
|
|
end
|
|
|
|
--- Returns the bitwise or of all its arguments.
|
|
--
|
|
-- REPLACEMENT: <code>a | b</code>
|
|
-- @param ... A variable number of Numbers to or.
|
|
-- @return The ored result.
|
|
function bor(a, b, ...)
|
|
a = a | b
|
|
if select("#", ...) > 0 then
|
|
return bor(a, ...)
|
|
else
|
|
return a
|
|
end
|
|
end
|
|
|
|
--- Returns the bitwise exclusive or of all its arguments.
|
|
--
|
|
-- REPLACEMENT: <code>a ~ b</code>
|
|
-- @param ... A variable number of Numbers to exclusive or.
|
|
-- @return The exclusive ored result.
|
|
function bxor(a, b, ...)
|
|
a = a ~ b
|
|
if select("#", ...) > 0 then
|
|
return bxor(a, ...)
|
|
else
|
|
return a
|
|
end
|
|
end
|
|
|
|
--- Returns <code>a</code> left-shifted by <code>b</code> places.
|
|
--
|
|
-- REPLACEMENT: <code>a << b</code>
|
|
-- @param a Number to perform the shift on.
|
|
-- @param b Number of shifts.
|
|
function lshift(a, b)
|
|
return a << b
|
|
end
|
|
|
|
--- Returns <code>a</code> right-shifted by <code>b</code> places.
|
|
--
|
|
-- REPLACEMENT: <code>a >> b</code>
|
|
-- @param a Number to perform the shift on.
|
|
-- @param b Number of shifts.
|
|
function rshift(a, b)
|
|
return a >> b
|
|
end
|
|
|
|
--- Returns the integer remainder of <code>a</code> divided by <code>b</code>.
|
|
--
|
|
-- REPLACEMENT: <code>a % b</code>
|
|
-- @param a Dividend.
|
|
-- @param b Divisor.
|
|
function mod(a, b)
|
|
return a % b
|
|
end
|
|
|
|
return _ENV
|