From 804565e6156079e5e70b5aa53800e6e51dc1a1cb Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Thu, 16 Dec 2021 13:20:05 +0100 Subject: [PATCH] feat(binary): add shiftRL() --- packages/binary/src/rotate.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/binary/src/rotate.ts b/packages/binary/src/rotate.ts index 8a48f78c50..dad1aae0cd 100644 --- a/packages/binary/src/rotate.ts +++ b/packages/binary/src/rotate.ts @@ -17,3 +17,12 @@ export const rotateLeft = (x: number, n: Bit) => */ export const rotateRight = (x: number, n: Bit) => ((x >>> n) | (x << (32 - n))) >>> 0; + +/** + * Shifts `x` by `n` bits left or right. If `n` >= 0, the value will be `>>>` + * shifted to right, if `n` < 0 the value will be shifted left. + * + * @param x + * @param n + */ +export const shiftRL = (x: number, n: number) => (n < 0 ? x << -n : x >>> n);