diff --git a/source/bits/index.ts b/source/bits/index.ts index d1eb53a..82f4d82 100644 --- a/source/bits/index.ts +++ b/source/bits/index.ts @@ -1,4 +1,6 @@ import and from './and' +import logicalShiftLeft from './logical-shift-left' +import logicalShiftRight from './logical-shift-right' import nand from './nand' import nor from './nor' import not from './not' @@ -16,6 +18,8 @@ import xor from './xor' export { and, + logicalShiftLeft, + logicalShiftRight, nand, nor, not, @@ -34,6 +38,8 @@ export { export default { and, + logicalShiftLeft, + logicalShiftRight, nand, nor, not, diff --git a/source/bits/logical-shift-left.test.ts b/source/bits/logical-shift-left.test.ts new file mode 100644 index 0000000..b183e49 --- /dev/null +++ b/source/bits/logical-shift-left.test.ts @@ -0,0 +1,12 @@ +import { Bits } from '../types' +import logicalShiftLeft from './logical-shift-left' + +test('LSHIFTL', () => { + const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] + const expected1: Bits = [0, 0, 0, 1, 1, 0, 1, 0] + expect(logicalShiftLeft(bits1)).toEqual(expected1) + + const bits2: Bits = [0, 0, 0, 1, 1, 1, 0, 0] + const expected2: Bits = [0, 0, 1, 1, 1, 0, 0, 0] + expect(logicalShiftLeft(bits2)).toEqual(expected2) +}) diff --git a/source/bits/logical-shift-left.ts b/source/bits/logical-shift-left.ts new file mode 100644 index 0000000..3cdcff1 --- /dev/null +++ b/source/bits/logical-shift-left.ts @@ -0,0 +1,21 @@ +import { Bit, Bits } from '../types' + +/** + * Logical Shift Left + * + * Shifts all given bits to left and returns the resulting bits + * + * @example + * logicalShiftLeft([1,0,1,1,0,1]) => [0,1,1,0,1,0] + * + * @param {Array} bits input data + * @return {Array} [LSHIFTL bits] + */ +export default (bits: Bits): Bits => { + const result: Bits = [] + + for (let i: number = 0; i < bits.length - 1; i++) result[i] = bits[i + 1] + result[bits.length - 1] = (0 as Bit) + + return result +} diff --git a/source/bits/logical-shift-right.test.ts b/source/bits/logical-shift-right.test.ts new file mode 100644 index 0000000..1397913 --- /dev/null +++ b/source/bits/logical-shift-right.test.ts @@ -0,0 +1,13 @@ +import { Bits } from '../types' +import logicalShiftRight from './logical-shift-right' + +test('LSHIFTR', () => { + const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] + const expected1: Bits = [0, 1, 0, 0, 0, 1, 1, 0] + expect(logicalShiftRight(bits1)).toEqual(expected1) + + const bits2: Bits = [0, 0, 0, 1, 1, 1, 0, 0] + const expected2: Bits = [0, 0, 0, 0, 1, 1, 1, 0] + expect(logicalShiftRight(bits2)).toEqual(expected2) +}) + diff --git a/source/bits/logical-shift-right.ts b/source/bits/logical-shift-right.ts new file mode 100644 index 0000000..24125ad --- /dev/null +++ b/source/bits/logical-shift-right.ts @@ -0,0 +1,21 @@ +import { Bit, Bits } from '../types' + +/** + * Logical Shift Right + * + * Shifts all given bits to right and returns the resulting bits + * + * @example + * logicalShiftRight([1,0,1,1,0,1]) => [0,1,0,1,1,0] + * + * @param {Array} bits input data + * @return {Array} [LSHIFTR bits] + */ +export default (bits: Bits): Bits => { + const result: Bits = [] + + result[0] = (0 as Bit) + for (let i: number = 0; i < bits.length - 1; i++) result[i + 1] = bits[i] + + return result +}