mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-24 08:32:02 +00:00
Added more bitwise functions [#CLICKHOUSE-2884].
This commit is contained in:
parent
a2d4f9907b
commit
910c542994
@ -20,9 +20,10 @@ namespace ErrorCodes
|
||||
}
|
||||
|
||||
|
||||
/** Арифметические функции: +, -, *, /, %,
|
||||
* intDiv (целочисленное деление), унарный минус.
|
||||
* Битовые функции: |, &, ^, ~.
|
||||
/** Arithmetic operations: +, -, *, /, %,
|
||||
* intDiv (integer division), unary minus.
|
||||
* Bitwise operations: |, &, ^, ~.
|
||||
* Etc.
|
||||
*/
|
||||
|
||||
template<typename A, typename B, typename Op, typename ResultType_ = typename Op::ResultType>
|
||||
@ -272,6 +273,32 @@ struct BitShiftRightImpl
|
||||
}
|
||||
};
|
||||
|
||||
template<typename A, typename B>
|
||||
struct BitRotateLeftImpl
|
||||
{
|
||||
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
|
||||
|
||||
template <typename Result = ResultType>
|
||||
static inline Result apply(A a, B b)
|
||||
{
|
||||
return (static_cast<Result>(a) << static_cast<Result>(b))
|
||||
| (static_cast<Result>(a) >> ((sizeof(Result) * 8) - static_cast<Result>(b)));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename A, typename B>
|
||||
struct BitRotateRightImpl
|
||||
{
|
||||
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
|
||||
|
||||
template <typename Result = ResultType>
|
||||
static inline Result apply(A a, B b)
|
||||
{
|
||||
return (static_cast<Result>(a) >> static_cast<Result>(b))
|
||||
| (static_cast<Result>(a) << ((sizeof(Result) * 8) - static_cast<Result>(b)));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename A, typename B>
|
||||
struct LeastBaseImpl
|
||||
@ -885,6 +912,8 @@ struct NameBitXor { static constexpr auto name = "bitXor"; };
|
||||
struct NameBitNot { static constexpr auto name = "bitNot"; };
|
||||
struct NameBitShiftLeft { static constexpr auto name = "bitShiftLeft"; };
|
||||
struct NameBitShiftRight { static constexpr auto name = "bitShiftRight"; };
|
||||
struct NameBitRotateLeft { static constexpr auto name = "bitRotateLeft"; };
|
||||
struct NameBitRotateRight { static constexpr auto name = "bitRotateRight"; };
|
||||
struct NameLeast { static constexpr auto name = "least"; };
|
||||
struct NameGreatest { static constexpr auto name = "greatest"; };
|
||||
|
||||
@ -903,6 +932,8 @@ using FunctionBitXor = FunctionBinaryArithmetic<BitXorImpl, NameBitXor>;
|
||||
using FunctionBitNot = FunctionUnaryArithmetic<BitNotImpl, NameBitNot, true>;
|
||||
using FunctionBitShiftLeft = FunctionBinaryArithmetic<BitShiftLeftImpl, NameBitShiftLeft>;
|
||||
using FunctionBitShiftRight = FunctionBinaryArithmetic<BitShiftRightImpl, NameBitShiftRight>;
|
||||
using FunctionBitRotateLeft = FunctionBinaryArithmetic<BitRotateLeftImpl, NameBitRotateLeft>;
|
||||
using FunctionBitRotateRight = FunctionBinaryArithmetic<BitRotateRightImpl, NameBitRotateRight>;
|
||||
using FunctionLeast = FunctionBinaryArithmetic<LeastImpl, NameLeast>;
|
||||
using FunctionGreatest = FunctionBinaryArithmetic<GreatestImpl, NameGreatest>;
|
||||
|
||||
|
@ -21,6 +21,8 @@ void registerFunctionsArithmetic(FunctionFactory & factory)
|
||||
factory.registerFunction<FunctionBitNot>();
|
||||
factory.registerFunction<FunctionBitShiftLeft>();
|
||||
factory.registerFunction<FunctionBitShiftRight>();
|
||||
factory.registerFunction<FunctionBitRotateLeft>();
|
||||
factory.registerFunction<FunctionBitRotateRight>();
|
||||
factory.registerFunction<FunctionLeast>();
|
||||
factory.registerFunction<FunctionGreatest>();
|
||||
}
|
||||
|
@ -0,0 +1,2 @@
|
||||
03
|
||||
C000000000000000
|
2
dbms/tests/queries/0_stateless/00438_bit_rotate.sql
Normal file
2
dbms/tests/queries/0_stateless/00438_bit_rotate.sql
Normal file
@ -0,0 +1,2 @@
|
||||
SELECT hex(bitRotateLeft(0x8000000000000001, 1));
|
||||
SELECT hex(bitRotateRight(0x8000000000000001, 1));
|
Loading…
Reference in New Issue
Block a user