diff --git a/source/bits/arithmetic-shift-left.test.ts b/source/bits/arithmetic-shift-left.test.ts new file mode 100644 index 0000000..4b5ba16 --- /dev/null +++ b/source/bits/arithmetic-shift-left.test.ts @@ -0,0 +1,12 @@ +import { Bits } from '../types' +import arithmeticShiftLeft from './arithmetic-shift-left' + +test('ASHIFTL', () => { + const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] + const expected1: Bits = [0, 0, 0, 1, 1, 0, 1, 0] + expect(arithmeticShiftLeft(bits1)).toEqual(expected1) + + const bits2: Bits = [0, 0, 0, 0, 1, 1, 0, 1] + const expected2: Bits = [0, 0, 0, 1, 1, 0, 1, 0] + expect(arithmeticShiftLeft(bits2)).toEqual(expected2) +}) diff --git a/source/bits/arithmetic-shift-left.ts b/source/bits/arithmetic-shift-left.ts new file mode 100644 index 0000000..2fbfed5 --- /dev/null +++ b/source/bits/arithmetic-shift-left.ts @@ -0,0 +1,19 @@ +import { Bits } from '../types' + +/** + * Arithmetic Shift Left + * + * @example + * arithmeticShiftLeft([1,0,1,1,0,1]) => [0,1,1,0,1,0] + * + * @param {Array} bits input data + * @return {Array} [ASHIFTL 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] = (0 as Bit) + + return result +} diff --git a/source/bits/arithmetic-shift-right.test.ts b/source/bits/arithmetic-shift-right.test.ts new file mode 100644 index 0000000..cbb7e9d --- /dev/null +++ b/source/bits/arithmetic-shift-right.test.ts @@ -0,0 +1,12 @@ +import { Bits } from '../types' +import arithmeticShiftRight from './arithmetic-shift-right' + +test('ASHIFTR', () => { + const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] + const expected1: Bits = [1, 1, 0, 0, 0, 1, 1, 0] + expect(arithmeticShiftRight(bits1)).toEqual(expected1) + + const bits2: Bits = [0, 0, 0, 0, 1, 1, 0, 1] + const expected2: Bits = [0, 0, 0, 0, 0, 1, 1, 0] + expect(arithmeticShiftRight(bits2)).toEqual(expected2) +}) diff --git a/source/bits/arithmetic-shift-right.ts b/source/bits/arithmetic-shift-right.ts new file mode 100644 index 0000000..6b730ce --- /dev/null +++ b/source/bits/arithmetic-shift-right.ts @@ -0,0 +1,19 @@ +import { Bit, Bits } from '../types' + +/** + * Arithmetic Shift Right + * + * @example + * arithmeticShiftRight([1,0,1,1,0,1]) => [1,1,0,1,1,0] + * + * @param {Array} bits input data + * @return {Array} [ASHIFTR bits] + */ +export default (bits: Bits): Bits => { + const result: Bits = [] + + result[0] = bits[0] + for (let i: number = 0; i < bits.length - 1; i++) result[i + 1] = bits[i] + + return result +} \ No newline at end of file diff --git a/source/bits/index.ts b/source/bits/index.ts index d1eb53a..9bbdd35 100644 --- a/source/bits/index.ts +++ b/source/bits/index.ts @@ -1,4 +1,6 @@ import and from './and' +import arithmeticShiftLeft from './arithmetic-shift-left' +import arithmeticShiftRight from './arithmetic-shift-right' import nand from './nand' import nor from './nor' import not from './not' @@ -16,6 +18,8 @@ import xor from './xor' export { and, + arithmeticShiftLeft, + arithmeticShiftRight, nand, nor, not, @@ -34,6 +38,8 @@ export { export default { and, + arithmeticShiftLeft, + arithmeticShiftRight, nand, nor, not,