Lua: bit.extract

This commit is contained in:
Ilari Liusvaara 2012-03-07 12:35:46 +02:00
parent 5d989b8929
commit 1a33ad9dc2
3 changed files with 52 additions and 0 deletions

View file

@ -2141,6 +2141,28 @@ Shift bits-bit (max 48, default 48) number arithmetically right by amount
The new bits are shifted in with copy of the high bit.
\end_layout
\begin_layout Subsubsection
bit.extract(number base[, number bit0[, number bit1,...]])
\end_layout
\begin_layout Standard
Returns number that has bit0-th bit as bit 0, bit1-th bit as 1 and so on.
\end_layout
\begin_layout Standard
Notes:
\end_layout
\begin_layout Itemize
Bit numbers up to 51 should work reliably (then things start falling apart
due to double precision issues).
\end_layout
\begin_layout Itemize
There are two special bit positions, true and false, standing for always
set bit and always clear bit.
\end_layout
\begin_layout Subsection
Table gui:
\end_layout

View file

@ -1041,6 +1041,19 @@ Shift bits-bit (max 48, default 48) number arithmetically right
by amount (default 1) places. The new bits are shifted in with
copy of the high bit.
8.2.10 bit.extract(number base[, number bit0[, number bit1,...]])
Returns number that has bit0-th bit as bit 0, bit1-th bit as 1
and so on.
Notes:
• Bit numbers up to 51 should work reliably (then things start
falling apart due to double precision issues).
• There are two special bit positions, true and false, standing
for always set bit and always clear bit.
8.3 Table gui:
Most of these functions can only be called in on_paint and

View file

@ -102,6 +102,23 @@ namespace
}
};
function_ptr_luafun lua_print("bit.extract", [](lua_State* LS, const std::string& fname) -> int {
uint64_t num = get_numeric_argument<uint64_t>(LS, 1, fname.c_str());
uint64_t ret = 0;
for(size_t i = 0;; i++) {
if(lua_isnumber(LS, i + 2)) {
uint8_t bit = get_numeric_argument<uint8_t>(LS, i + 2, fname.c_str());
ret |= (((num >> bit) & 1) << i);
} else if(lua_isboolean(LS, i + 2)) {
if(lua_toboolean(LS, i + 2))
ret |= (1ULL << i);
} else
break;
}
lua_pushnumber(LS, ret);
return 1;
});
lua_symmetric_bitwise<combine_none, BITWISE_MASK> bit_none("bit.none");
lua_symmetric_bitwise<combine_none, BITWISE_MASK> bit_bnot("bit.bnot");
lua_symmetric_bitwise<combine_any, 0> bit_any("bit.any");