From f8abc9665446752acfd0062bf6b80e74467a88e8 Mon Sep 17 00:00:00 2001 From: 0xflotus <0xflotus@gmail.com> Date: Tue, 13 Oct 2020 12:49:01 +0200 Subject: [PATCH] feature(#44): Added Circular Shift --- source/bits/circular-shift-left.test.ts | 12 ++++++++++++ source/bits/circular-shift-left.ts | 19 +++++++++++++++++++ source/bits/circular-shift-right.test.ts | 12 ++++++++++++ source/bits/circular-shift-right.ts | 19 +++++++++++++++++++ source/bits/index.ts | 6 ++++++ 5 files changed, 68 insertions(+) create mode 100644 source/bits/circular-shift-left.test.ts create mode 100644 source/bits/circular-shift-left.ts create mode 100644 source/bits/circular-shift-right.test.ts create mode 100644 source/bits/circular-shift-right.ts diff --git a/source/bits/circular-shift-left.test.ts b/source/bits/circular-shift-left.test.ts new file mode 100644 index 0000000..b7de121 --- /dev/null +++ b/source/bits/circular-shift-left.test.ts @@ -0,0 +1,12 @@ +import { Bits } from '../types' +import circularShiftLeft from './circular-shift-left' + +test('CSHIFTL', () => { + const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] + const expected1: Bits = [0, 0, 0, 1, 1, 0, 1, 1] + expect(circularShiftLeft(bits1)).toEqual(expected1) + + const bits2: Bits = [0, 0, 0, 1, 1, 1, 1, 1] + const expected2: Bits = [0, 0, 1, 1, 1, 1, 1, 0] + expect(circularShiftLeft(bits2)).toEqual(expected2) +}) diff --git a/source/bits/circular-shift-left.ts b/source/bits/circular-shift-left.ts new file mode 100644 index 0000000..6d4b418 --- /dev/null +++ b/source/bits/circular-shift-left.ts @@ -0,0 +1,19 @@ +import { Bits } from '../types' + +/** + * Circular Shift Left + * + * @example + * circularShiftLeft([1,0,1,1,0,1]) => [0,1,1,0,1,1] + * + * @param {Array} bits input data + * @return {Array} [CSHIFTL bits] + */ +export default (bits: Bits): Bits => { + const result: Bits = [] + + for (let i: number = 1; i < bits.length; i++) result[i - 1] = bits[i] + result[bits.length - 1] = bits[0] + + return result +} diff --git a/source/bits/circular-shift-right.test.ts b/source/bits/circular-shift-right.test.ts new file mode 100644 index 0000000..4eb12bc --- /dev/null +++ b/source/bits/circular-shift-right.test.ts @@ -0,0 +1,12 @@ +import { Bits } from '../types' +import circularShiftRight from './circular-shift-right' + +test('CSHIFTR', () => { + const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] + const expected1: Bits = [1, 1, 0, 0, 0, 1, 1, 0] + expect(circularShiftRight(bits1)).toEqual(expected1) + + const bits2: Bits = [0, 0, 0, 1, 1, 1, 1, 1] + const expected2: Bits = [1, 0, 0, 0, 1, 1, 1, 1] + expect(circularShiftRight(bits2)).toEqual(expected2) +}) diff --git a/source/bits/circular-shift-right.ts b/source/bits/circular-shift-right.ts new file mode 100644 index 0000000..f5a2dad --- /dev/null +++ b/source/bits/circular-shift-right.ts @@ -0,0 +1,19 @@ +import { Bits } from '../types' + +/** + * Circular Shift Right + * + * @example + * circularShiftRight([1,0,1,1,0,1]) => [1,1,0,1,1,0] + * + * @param {Array} bits input data + * @return {Array} [CSHIFTR bits] + */ +export default (bits: Bits): Bits => { + const result: Bits = [] + + result[0] = bits.pop() + for (let i: number = 0; i < bits.length; i++) result[i + 1] = bits[i] + + return result +} diff --git a/source/bits/index.ts b/source/bits/index.ts index d1eb53a..e68e1af 100644 --- a/source/bits/index.ts +++ b/source/bits/index.ts @@ -1,4 +1,6 @@ import and from './and' +import circularShiftLeft from './circular-shift-left' +import circularShiftRight from './circular-shift-right' import nand from './nand' import nor from './nor' import not from './not' @@ -16,6 +18,8 @@ import xor from './xor' export { and, + circularShiftLeft, + circularShiftRight, nand, nor, not, @@ -34,6 +38,8 @@ export { export default { and, + circularShiftLeft, + circularShiftRight, nand, nor, not,