Skip to content

added logical shift operations #45

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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/lshiftl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Bits } from '../types'
import lshiftl from './lshiftl'

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(lshiftl(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(lshiftl(bits2)).toEqual(expected2)
})
21 changes: 21 additions & 0 deletions source/bits/lshiftl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Bit, Bits } from '../types'

/**
* Logical Shift Left
*
* Shifts all given bits to left and returns the resulting bits
*
* @example
* lshiftl([1,0,1,1,0,1]) => [0,1,1,0,1,0]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xflotus can you make a case for this not returning [1,0,1,1,0,1,0] instead? bitwise basically supports arbitrary size bit arrays, so perhaps this is an unexpected result

If you have a particular use-case in mind for the non-size-increasing version, we could also add a flag for this behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 6 Bits in the example and you are referring to 7 Bits. Which case should it return?

*
* @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.push(bits[i + 1])
result.push(<Bit>0)

return result
}
13 changes: 13 additions & 0 deletions source/bits/lshiftr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Bits } from '../types'
import lshiftr from './lshiftr'

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(lshiftr(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(lshiftr(bits2)).toEqual(expected2)
})

21 changes: 21 additions & 0 deletions source/bits/lshiftr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Bit, Bits } from '../types'

/**
* Logical Shift Right
*
* Shifts all given bits to right and returns the resulting bits
*
* @example
* lshiftr([1,0,1,1,0,1]) => [0,1,0,1,1,0]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not as concerned about this one, but I suppose it’s possible to argue that the leading 0 should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure?

*
* @param {Array} bits input data
* @return {Array} [LSHIFTR bits]
*/
export default (bits: Bits): Bits => {
const result: Bits = []

result.push(<Bit>0)
for (let i: number = 0; i < bits.length - 1; i++) result.push(bits[i])

return result
}