Skip to content

Commit

Permalink
Add support for bitwise AND, OR, and XOR
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake-Madden committed Feb 26, 2024
1 parent 7756972 commit 87fb541
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 20 deletions.
3 changes: 3 additions & 0 deletions TinyExprChanges.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ The following are changes from the original TinyExpr C library:
- Added new built-in functions:
- `and`: returns true (i.e., non-zero) if all conditions are true (accepts 1-7 arguments).
- `average`: returns the mean for a range of values (accepts 1-7 arguments).
- `bitand`: bitwise AND.
- `bitlshift`: left shift operator.
Negative shift amount arguments (similar to *Excel*) is supported.
- `bitor`: bitwise OR.
- `bitrshift`: right shift operator.
Negative shift amount arguments (similar to *Excel*) is supported.
- `bitxor`: bitwise XOR.
- `cot`: returns the cotangent of an angle.
- `combin`: alias for `ncr()`, like the *Excel* function.
- `clamp`: constrains a value to a range.
Expand Down
5 changes: 5 additions & 0 deletions docs/manual/compile-time-options.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Compile with `TE_FLOAT` defined to use `float` instead.

Refer to [floating-point numbers](#fp-numbers) for more information.

## `TE_BITWISE_OPERATIONS` {-#te-bitwise-ops}

By default, the operators `&`, `|`, and `^` represent logical AND, logical OR, and exponentiation. If `TE_BITWISE_OPERATIONS` is defined,
then they will represent bitwise AND, OR, and XOR.

## `TE_NO_BOOKKEEPING` {-}

By default, the parser will keep track of all functions and variables used in the last expression it evaluated.
Expand Down
9 changes: 6 additions & 3 deletions docs/manual/functions.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ The following built-in functions are available:
| ASIN(Number) | Returns the arcsine, or inverse sine function, of *Number*, where -1 <= *Number* <= 1. The arcsine is the angle whose sine is *Number*. The returned angle is given in radians where -pi/2 <= angle <= pi/2. |
| ATAN(x) | Returns the principal value of the arc tangent of *x*, expressed in radians.. |
| ATAN2(y, x) | Returns the principal value of the arc tangent of *y*,*x*, expressed in radians. |
| BITAND(Number1, Number2) | Returns a bitwise 'AND' of two (integral) numbers. (Both numbers must be positive.) |
| BITLSHIFT(Number, ShiftAmount) | Returns *Number* left shifted by the specified number (*ShiftAmount*) of bits. |
| BITRSHIFT(Number, ShiftAmount) |Returns *Number* right shifted by the specified number (*ShiftAmount*) of bits. |
| BITRSHIFT(Number, ShiftAmount) | Returns *Number* right shifted by the specified number (*ShiftAmount*) of bits. |
| BITOR(Number1, Number2) | Returns a bitwise 'OR' of two (integral) numbers. (Both numbers must be positive.) |
| BITXOR(Number1, Number2) | Returns a bitwise 'XOR' of two (integral) numbers. (Both numbers must be positive.) |
| CEIL(Number) | Smallest integer not less than *Number*.<br>\linebreak `CEIL(-3.2)` = -3<br>\linebreak `CEIL(3.2)` = 4 |
| CLAMP(Number, Start, End) | Constrains *Number* within the range of *Start* and *End*. |
| COMBIN(Number, NumberChosen) | Returns the number of combinations for a given number (*NumberChosen*) of items from *Number* of items. Note that for combinations, order of items is not important. |
Expand All @@ -24,8 +27,8 @@ The following built-in functions are available:
| FLOOR(Number) | Returns the largest integer not greater than *Number*.<br>\linebreak `FLOOR(-3.2)` = -4<br>\linebreak `FLOOR(3.2)` = 3 |
| LN(Number) | Natural logarithm of *Number* (base Euler). |
| LOG10(Number) | Common logarithm of *Number* (base 10). |
| MIN(Value1, Value2, ...) | Returns the lowest value from a specified range of values. |
| MAX(Value1, Value2, ...) | Returns the highest value from a specified range of values. |
| MIN(Number1, Number2, ...) | Returns the lowest value from a specified range of values. |
| MAX(Number1, Number2, ...) | Returns the highest value from a specified range of values. |
| MOD(Number, Divisor) | Returns the remainder after *Number* is divided by *Divisor*. The result has the same sign as divisor. |
| NCR(Number, NumberChosen) | Alias for `COMBIN()`. |
| NPR(Number, NumberChosen) | Alias for `PERMUT()`. |
Expand Down
11 changes: 7 additions & 4 deletions docs/manual/operators.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The following operators\index{operators} are supported within math expressions:
| % | Modulus: Divides two values and returns the remainder. |
| + | Addition. |
| \- | Subtraction. |
| ^ | Exponentiation. The number in front of ^ is the base, the number after it is the power to raise it to. |
| ^ | Either exponentiation or bitwise XOR. Exponentiation is the default; define `TE_BITWISE_OPERATIONS` to enable bitwise behavior. For exponentiation, the number in front of ^ is the base, the number after it is the power to raise it to. |
| ** | Exponentiation. (This is an alias for ^) |
| < | Less than. |
| \> | Greater than. |
Expand All @@ -20,9 +20,9 @@ The following operators\index{operators} are supported within math expressions:
| \=\= | Equals. (This is an alias for \=) |
| <> | Not equal to. |
| \!\= | Not equal to. (This is an alias for <>) |
| & | Logical conjunction (AND). |
| && | Logical conjunction (AND). |
| \| | Logical alternative (OR). |
| & | Either logical or bitwise conjunction (AND). Logical is the default, define `TE_BITWISE_OPERATIONS` to enable bitwise behavior. |
| && | Either logical conjunction (AND). |
| \| | Logical or bitwise alternative (OR). Logical is the default, define `TE_BITWISE_OPERATIONS` to enable bitwise behavior. |
| \|\| | Logical alternative (OR). |
| ( ) | Groups sub-expressions, overriding the order of operations. |
| << | Bitwise left shift. |
Expand All @@ -46,6 +46,9 @@ For operators, the order of precedence is:
| \= and \!\= | Equality comparisons. |
| && | Logical conjunction (AND). |
| \|\| | Logical alternative (OR). |
| & | Bitwise AND (if `TE_BITWISE_OPERATIONS` is defined). |
| ^ | Bitwise XOR (if `TE_BITWISE_OPERATIONS` is defined). |
| \| | Bitwise OR (if `TE_BITWISE_OPERATIONS` is defined). |
:::

For example, the following:
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ endif(TE_FLOAT)
if(TE_POW_FROM_RIGHT)
add_definitions(-DTE_POW_FROM_RIGHT)
endif(TE_POW_FROM_RIGHT)
if(TE_BITWISE_OPERATIONS)
add_definitions(-TE_BITWISE_OPERATIONS)
endif(TE_BITWISE_OPERATIONS)

# place Catch2 at the same folder level as this repo if it isn't installed
# (you will need to do this on Windows or macOS or if version 3 of Catch2 isn't installed)
Expand Down
Loading

0 comments on commit 87fb541

Please sign in to comment.