From 228e16338aa5ca3da4bc5e2684e344b4a2b7a20c Mon Sep 17 00:00:00 2001 From: 0xflotus <0xflotus@gmail.com> Date: Tue, 13 Oct 2020 13:02:44 +0200 Subject: [PATCH 1/5] added arithmetic shift operations --- source/bits/ashiftl.test.ts | 12 ++++++++++++ source/bits/ashiftl.ts | 19 +++++++++++++++++++ source/bits/ashiftr.test.ts | 12 ++++++++++++ source/bits/ashiftr.ts | 19 +++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 source/bits/ashiftl.test.ts create mode 100644 source/bits/ashiftl.ts create mode 100644 source/bits/ashiftr.test.ts create mode 100644 source/bits/ashiftr.ts diff --git a/source/bits/ashiftl.test.ts b/source/bits/ashiftl.test.ts new file mode 100644 index 0000000..0255887 --- /dev/null +++ b/source/bits/ashiftl.test.ts @@ -0,0 +1,12 @@ +import { Bits } from '../types' +import ashiftl from './ashiftl' + +test('ASHIFTL', () => { + const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] + const expected1: Bits = [1, 1, 0, 0, 0, 1, 1, 0] + expect(ashiftl(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(ashiftl(bits2)).toEqual(expected2) +}) diff --git a/source/bits/ashiftl.ts b/source/bits/ashiftl.ts new file mode 100644 index 0000000..794aeb1 --- /dev/null +++ b/source/bits/ashiftl.ts @@ -0,0 +1,19 @@ +import { Bits } from '../types' + +/** + * Arithmetic Shift Left + * + * @example + * ashiftl([1,0,1,1,0,1]) => [1,1,0,1,1,0] + * + * @param {Array} bits input data + * @return {Array} [ASHIFTL bits] + */ +export default (bits: Bits): Bits => { + const result: Bits = [] + + result.push(bits[0]) + for (let i: number = 0; i < bits.length - 1; i++) result.push(bits[i]) + + return result +} diff --git a/source/bits/ashiftr.test.ts b/source/bits/ashiftr.test.ts new file mode 100644 index 0000000..acdbae7 --- /dev/null +++ b/source/bits/ashiftr.test.ts @@ -0,0 +1,12 @@ +import { Bits } from '../types' +import ashiftr from './ashiftr' + +test('ASHIFTR', () => { + const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] + const expected1: Bits = [0, 0, 0, 1, 1, 0, 1, 0] + expect(ashiftr(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(ashiftr(bits2)).toEqual(expected2) +}) diff --git a/source/bits/ashiftr.ts b/source/bits/ashiftr.ts new file mode 100644 index 0000000..11a0d53 --- /dev/null +++ b/source/bits/ashiftr.ts @@ -0,0 +1,19 @@ +import { Bit, Bits } from '../types' + +/** + * Arithmetic Shift Right + * + * @example + * ashiftr([1,0,1,1,0,1]) => [0,1,1,0,1,0] + * + * @param {Array} bits input data + * @return {Array} [ASHIFTR bits] + */ +export default (bits: Bits): Bits => { + const result: Bits = [] + + for (let i: number = 1; i < bits.length; i++) result.push(bits[i]) + result.push(0) + + return result +} From 95291694e571eeeaa964cee066d7315aafa6014b Mon Sep 17 00:00:00 2001 From: 0xflotus <0xflotus@gmail.com> Date: Tue, 13 Oct 2020 19:26:56 +0200 Subject: [PATCH 2/5] renamed files --- .../bits/{ashiftl.test.ts => arithmetic-shift-left.test.ts} | 0 source/bits/{ashiftl.ts => arithmetic-shift-left.ts} | 2 +- .../{ashiftr.test.ts => arithmetic-shift-right.test.ts} | 6 +++--- source/bits/{ashiftr.ts => arithmetic-shift-right.ts} | 0 source/bits/index.ts | 3 +++ 5 files changed, 7 insertions(+), 4 deletions(-) rename source/bits/{ashiftl.test.ts => arithmetic-shift-left.test.ts} (100%) rename source/bits/{ashiftl.ts => arithmetic-shift-left.ts} (86%) rename source/bits/{ashiftr.test.ts => arithmetic-shift-right.test.ts} (59%) rename source/bits/{ashiftr.ts => arithmetic-shift-right.ts} (100%) diff --git a/source/bits/ashiftl.test.ts b/source/bits/arithmetic-shift-left.test.ts similarity index 100% rename from source/bits/ashiftl.test.ts rename to source/bits/arithmetic-shift-left.test.ts diff --git a/source/bits/ashiftl.ts b/source/bits/arithmetic-shift-left.ts similarity index 86% rename from source/bits/ashiftl.ts rename to source/bits/arithmetic-shift-left.ts index 794aeb1..f3a8434 100644 --- a/source/bits/ashiftl.ts +++ b/source/bits/arithmetic-shift-left.ts @@ -4,7 +4,7 @@ import { Bits } from '../types' * Arithmetic Shift Left * * @example - * ashiftl([1,0,1,1,0,1]) => [1,1,0,1,1,0] + * arithmeticShiftLeft([1,0,1,1,0,1]) => [1,1,0,1,1,0] * * @param {Array} bits input data * @return {Array} [ASHIFTL bits] diff --git a/source/bits/ashiftr.test.ts b/source/bits/arithmetic-shift-right.test.ts similarity index 59% rename from source/bits/ashiftr.test.ts rename to source/bits/arithmetic-shift-right.test.ts index acdbae7..01b305a 100644 --- a/source/bits/ashiftr.test.ts +++ b/source/bits/arithmetic-shift-right.test.ts @@ -1,12 +1,12 @@ import { Bits } from '../types' -import ashiftr from './ashiftr' +import arithmeticShiftRight from './arithmetic-shift-right' test('ASHIFTR', () => { const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] const expected1: Bits = [0, 0, 0, 1, 1, 0, 1, 0] - expect(ashiftr(bits1)).toEqual(expected1) + expect(arithmeticShiftRight(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(ashiftr(bits2)).toEqual(expected2) + expect(arithmeticShiftRight(bits2)).toEqual(expected2) }) diff --git a/source/bits/ashiftr.ts b/source/bits/arithmetic-shift-right.ts similarity index 100% rename from source/bits/ashiftr.ts rename to source/bits/arithmetic-shift-right.ts diff --git a/source/bits/index.ts b/source/bits/index.ts index d1eb53a..90b6496 100644 --- a/source/bits/index.ts +++ b/source/bits/index.ts @@ -1,4 +1,5 @@ import and from './and' +import arithmeticShiftRight from './arithmetic-shift-right' import nand from './nand' import nor from './nor' import not from './not' @@ -16,6 +17,7 @@ import xor from './xor' export { and, + arithmeticShiftRight, nand, nor, not, @@ -34,6 +36,7 @@ export { export default { and, + arithmeticShiftRight, nand, nor, not, From e94761f59ed0e67139c2e351f825fd4996b688d2 Mon Sep 17 00:00:00 2001 From: 0xflotus <0xflotus@gmail.com> Date: Tue, 13 Oct 2020 19:28:27 +0200 Subject: [PATCH 3/5] cast --- source/bits/arithmetic-shift-right.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/bits/arithmetic-shift-right.ts b/source/bits/arithmetic-shift-right.ts index 11a0d53..5411143 100644 --- a/source/bits/arithmetic-shift-right.ts +++ b/source/bits/arithmetic-shift-right.ts @@ -4,7 +4,7 @@ import { Bit, Bits } from '../types' * Arithmetic Shift Right * * @example - * ashiftr([1,0,1,1,0,1]) => [0,1,1,0,1,0] + * arithmeticShiftRight([1,0,1,1,0,1]) => [0,1,1,0,1,0] * * @param {Array} bits input data * @return {Array} [ASHIFTR bits] @@ -13,7 +13,7 @@ export default (bits: Bits): Bits => { const result: Bits = [] for (let i: number = 1; i < bits.length; i++) result.push(bits[i]) - result.push(0) + result.push(0 as Bit) return result } From 311a152543691a6ed44f5d23e8d72385eac362d9 Mon Sep 17 00:00:00 2001 From: 0xflotus <0xflotus@gmail.com> Date: Tue, 13 Oct 2020 19:30:09 +0200 Subject: [PATCH 4/5] added to index.ts --- source/bits/arithmetic-shift-left.test.ts | 6 +++--- source/bits/index.ts | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/source/bits/arithmetic-shift-left.test.ts b/source/bits/arithmetic-shift-left.test.ts index 0255887..7729c1b 100644 --- a/source/bits/arithmetic-shift-left.test.ts +++ b/source/bits/arithmetic-shift-left.test.ts @@ -1,12 +1,12 @@ import { Bits } from '../types' -import ashiftl from './ashiftl' +import arithmeticShiftLeft from './arithmetic-shift-left' test('ASHIFTL', () => { const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] const expected1: Bits = [1, 1, 0, 0, 0, 1, 1, 0] - expect(ashiftl(bits1)).toEqual(expected1) + expect(arithmeticShiftLeft(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(ashiftl(bits2)).toEqual(expected2) + expect(arithmeticShiftLeft(bits2)).toEqual(expected2) }) diff --git a/source/bits/index.ts b/source/bits/index.ts index 90b6496..9bbdd35 100644 --- a/source/bits/index.ts +++ b/source/bits/index.ts @@ -1,4 +1,5 @@ import and from './and' +import arithmeticShiftLeft from './arithmetic-shift-left' import arithmeticShiftRight from './arithmetic-shift-right' import nand from './nand' import nor from './nor' @@ -17,6 +18,7 @@ import xor from './xor' export { and, + arithmeticShiftLeft, arithmeticShiftRight, nand, nor, @@ -36,6 +38,7 @@ export { export default { and, + arithmeticShiftLeft, arithmeticShiftRight, nand, nor, From 5e8237aeb6008d65d90dca4012c8a3f2ee1ee629 Mon Sep 17 00:00:00 2001 From: 0xflotus <0xflotus@gmail.com> Date: Tue, 13 Oct 2020 23:12:54 +0200 Subject: [PATCH 5/5] exchanged implementation ashiftl <-> ashiftr --- source/bits/arithmetic-shift-left.test.ts | 4 ++-- source/bits/arithmetic-shift-left.ts | 6 +++--- source/bits/arithmetic-shift-right.test.ts | 4 ++-- source/bits/arithmetic-shift-right.ts | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/source/bits/arithmetic-shift-left.test.ts b/source/bits/arithmetic-shift-left.test.ts index 7729c1b..4b5ba16 100644 --- a/source/bits/arithmetic-shift-left.test.ts +++ b/source/bits/arithmetic-shift-left.test.ts @@ -3,10 +3,10 @@ import arithmeticShiftLeft from './arithmetic-shift-left' test('ASHIFTL', () => { const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] - const expected1: Bits = [1, 1, 0, 0, 0, 1, 1, 0] + 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, 0, 0, 1, 1, 0] + 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 index f3a8434..2fbfed5 100644 --- a/source/bits/arithmetic-shift-left.ts +++ b/source/bits/arithmetic-shift-left.ts @@ -4,7 +4,7 @@ import { Bits } from '../types' * Arithmetic Shift Left * * @example - * arithmeticShiftLeft([1,0,1,1,0,1]) => [1,1,0,1,1,0] + * arithmeticShiftLeft([1,0,1,1,0,1]) => [0,1,1,0,1,0] * * @param {Array} bits input data * @return {Array} [ASHIFTL bits] @@ -12,8 +12,8 @@ import { Bits } from '../types' export default (bits: Bits): Bits => { const result: Bits = [] - result.push(bits[0]) - for (let i: number = 0; i < bits.length - 1; i++) result.push(bits[i]) + 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 index 01b305a..cbb7e9d 100644 --- a/source/bits/arithmetic-shift-right.test.ts +++ b/source/bits/arithmetic-shift-right.test.ts @@ -3,10 +3,10 @@ import arithmeticShiftRight from './arithmetic-shift-right' test('ASHIFTR', () => { const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1] - const expected1: Bits = [0, 0, 0, 1, 1, 0, 1, 0] + 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, 1, 1, 0, 1, 0] + 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 index 5411143..6b730ce 100644 --- a/source/bits/arithmetic-shift-right.ts +++ b/source/bits/arithmetic-shift-right.ts @@ -4,7 +4,7 @@ import { Bit, Bits } from '../types' * Arithmetic Shift Right * * @example - * arithmeticShiftRight([1,0,1,1,0,1]) => [0,1,1,0,1,0] + * arithmeticShiftRight([1,0,1,1,0,1]) => [1,1,0,1,1,0] * * @param {Array} bits input data * @return {Array} [ASHIFTR bits] @@ -12,8 +12,8 @@ import { Bit, Bits } from '../types' export default (bits: Bits): Bits => { const result: Bits = [] - for (let i: number = 1; i < bits.length; i++) result.push(bits[i]) - result.push(0 as Bit) + 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