mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-11-22 15:18:50 +08:00
math: Add bitwise and/or functions
Just as `math "bitand(5,3)"` and `math "bitor(6,2)"`. These cast to long long before doing their thing, so they truncate to an integer, producing weird results with floats. That's to be expected because float representation is *very* different, and performing bitwise operations on floats feels quite useless. Fixes #7281.
This commit is contained in:
parent
6ec6076c16
commit
5b1c000a2e
|
@ -75,6 +75,7 @@ Functions
|
|||
- ``asin``
|
||||
- ``atan``
|
||||
- ``atan2``
|
||||
- ``bitand``, ``bitor`` and ``bitxor`` to perform bitwise operations. These will throw away any non-integer parts and interpret the rest as an int.
|
||||
- ``ceil``
|
||||
- ``cos``
|
||||
- ``cosh``
|
||||
|
@ -114,6 +115,10 @@ Examples
|
|||
|
||||
``math 0xFF`` outputs 255, ``math 0 x 3`` outputs 0 (because it computes 0 multiplied by 3).
|
||||
|
||||
``math "bitand(0xFE, 0x2e)"`` outputs 46.
|
||||
|
||||
``math "bitor(9,2)"`` outputs 11.
|
||||
|
||||
Compatibility notes
|
||||
-------------------
|
||||
|
||||
|
|
|
@ -167,6 +167,19 @@ static double ncr(double n, double r) {
|
|||
|
||||
static double npr(double n, double r) { return ncr(n, r) * fac(r); }
|
||||
|
||||
|
||||
static constexpr double bit_and(double a, double b) {
|
||||
return static_cast<double>(static_cast<long long>(a) & static_cast<long long>(b));
|
||||
}
|
||||
|
||||
static constexpr double bit_or(double a, double b) {
|
||||
return static_cast<double>(static_cast<long long>(a) | static_cast<long long>(b));
|
||||
}
|
||||
|
||||
static constexpr double bit_xor(double a, double b) {
|
||||
return static_cast<double>(static_cast<long long>(a) ^ static_cast<long long>(b));
|
||||
}
|
||||
|
||||
static const te_builtin functions[] = {
|
||||
/* must be in alphabetical order */
|
||||
{"abs", reinterpret_cast<const void *>(static_cast<te_fun1>(fabs)), TE_FUNCTION1},
|
||||
|
@ -174,6 +187,9 @@ static const te_builtin functions[] = {
|
|||
{"asin", reinterpret_cast<const void *>(static_cast<te_fun1>(asin)), TE_FUNCTION1},
|
||||
{"atan", reinterpret_cast<const void *>(static_cast<te_fun1>(atan)), TE_FUNCTION1},
|
||||
{"atan2", reinterpret_cast<const void *>(static_cast<te_fun2>(atan2)), TE_FUNCTION2},
|
||||
{"bitand", reinterpret_cast<const void *>(static_cast<te_fun2>(bit_and)), TE_FUNCTION2},
|
||||
{"bitor", reinterpret_cast<const void *>(static_cast<te_fun2>(bit_or)), TE_FUNCTION2},
|
||||
{"bitxor", reinterpret_cast<const void *>(static_cast<te_fun2>(bit_xor)), TE_FUNCTION2},
|
||||
{"ceil", reinterpret_cast<const void *>(static_cast<te_fun1>(ceil)), TE_FUNCTION1},
|
||||
{"cos", reinterpret_cast<const void *>(static_cast<te_fun1>(cos)), TE_FUNCTION1},
|
||||
{"cosh", reinterpret_cast<const void *>(static_cast<te_fun1>(cosh)), TE_FUNCTION1},
|
||||
|
|
|
@ -140,3 +140,17 @@ math "42 >= 1337"
|
|||
# CHECKERR: math: Error: Logical operations are not supported, use `test` instead
|
||||
# CHECKERR: '42 >= 1337'
|
||||
# CHECKERR: ^
|
||||
|
||||
math "bitand(0xFE, 1)"
|
||||
# CHECK: 0
|
||||
math "bitor(0xFE, 1)"
|
||||
# CHECK: 255
|
||||
math "bitxor(5, 1)"
|
||||
# CHECK: 4
|
||||
math "bitand(5.5, 2)"
|
||||
# CHECK: 0
|
||||
math "bitand(5.5, 1)"
|
||||
# CHECK: 1
|
||||
|
||||
math "bitor(37 ^ 5, 255)"
|
||||
# CHECK: 69343999
|
||||
|
|
Loading…
Reference in New Issue
Block a user