Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added arithmetic shift operations #47

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions source/bits/arithmetic-shift-left.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
19 changes: 19 additions & 0 deletions source/bits/arithmetic-shift-left.ts
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions source/bits/arithmetic-shift-right.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
19 changes: 19 additions & 0 deletions source/bits/arithmetic-shift-right.ts
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions source/bits/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -16,6 +18,8 @@ import xor from './xor'

export {
and,
arithmeticShiftLeft,
arithmeticShiftRight,
nand,
nor,
not,
Expand All @@ -34,6 +38,8 @@ export {

export default {
and,
arithmeticShiftLeft,
arithmeticShiftRight,
nand,
nor,
not,
Expand Down