Add bitwise logical functions

This commit is contained in:
Glen Whitney 2019-07-29 13:22:15 -04:00
parent 1114df2e13
commit a5bf64b061
2 changed files with 114 additions and 5 deletions

View File

@ -5775,7 +5775,78 @@ x
x
\emph default
is given in radians.
\end_layout
\begin_layout Description
\series medium
int
\begin_inset space ~
\end_inset
\series default
bitand
\series medium
(int
\emph on
\begin_inset space ~
\end_inset
v1,
\begin_inset space ~
\end_inset
...
\emph default
) evaluates to the bitwise
\begin_inset Quotes eld
\end_inset
and
\begin_inset Quotes erd
\end_inset
of all the supplied values.
\end_layout
\begin_layout Description
\series medium
int
\begin_inset space ~
\end_inset
\series default
bitor
\series medium
(int
\emph on
\begin_inset space ~
\end_inset
v
\emph default
1,
\emph on
\begin_inset space ~
\end_inset
\emph default
...) evaluates to the bitwise
\begin_inset Quotes eld
\end_inset
or
\begin_inset Quotes erd
\end_inset
of all the supplied values.
\end_layout
\begin_layout Description

View File

@ -511,6 +511,42 @@ static Token z_func(int argc, const Token argv[])
}
/*}}}*/
typedef enum { LOG_AND, LOG_OR } LogicalFunction;
static Token bitwise_func(int argc, const Token argv[], LogicalFunction lop)
{
Token result;
int accum;
if (lop == LOG_AND) accum = -1;
else accum = 0;
for (size_t i = 0; i < argc; ++i) {
if (!INTPATIBLE(argv[i]))
{
result.type = EEK;
result.u.err =
mystrmalloc(_("Bitwise functions operate only on integers."));
return result;
}
int val = 0;
if (argv[i].type == INT) val = argv[i].u.integer;
if (lop == LOG_AND) accum = accum & val;
else accum = accum | val;
}
result.type = INT;
result.u.integer = accum;
return result;
}
static Token bitand_func(int argc, const Token argv[])
{
return bitwise_func(argc, argv, LOG_AND);
}
static Token bitor_func(int argc, const Token argv[])
{
return bitwise_func(argc, argv, LOG_OR);
}
/* e */ /*{{{*/
static Token e_func(int argc, const Token argv[])
{
@ -1451,6 +1487,8 @@ Tfunc tfunc[]=
{ "substr", substr_func },
{ "strptime", strptime_func },
{ "time", time_func },
{ "bitand", bitand_func },
{ "bitor", bitor_func },
{ "", (Token (*)(int, const Token[]))0 }
};
/*}}}*/