Skip to content

Commit db4f734

Browse files
0xflotusFlorianWendelborn
authored andcommitted
feature(#44): Added Circular Shift
1 parent f62d20c commit db4f734

5 files changed

+68
-0
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Bits } from '../types'
2+
import circularShiftLeft from './circular-shift-left'
3+
4+
test('CSHIFTL', () => {
5+
const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1]
6+
const expected1: Bits = [0, 0, 0, 1, 1, 0, 1, 1]
7+
expect(circularShiftLeft(bits1)).toEqual(expected1)
8+
9+
const bits2: Bits = [0, 0, 0, 1, 1, 1, 1, 1]
10+
const expected2: Bits = [0, 0, 1, 1, 1, 1, 1, 0]
11+
expect(circularShiftLeft(bits2)).toEqual(expected2)
12+
})

source/bits/circular-shift-left.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Bits } from '../types'
2+
3+
/**
4+
* Circular Shift Left
5+
*
6+
* @example
7+
* circularShiftLeft([1,0,1,1,0,1]) => [0,1,1,0,1,1]
8+
*
9+
* @param {Array} bits input data
10+
* @return {Array} [CSHIFTL bits]
11+
*/
12+
export default (bits: Bits): Bits => {
13+
const result: Bits = []
14+
15+
for (let i: number = 1; i < bits.length; i++) result[i - 1] = bits[i]
16+
result[bits.length - 1] = bits[0]
17+
18+
return result
19+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Bits } from '../types'
2+
import circularShiftRight from './circular-shift-right'
3+
4+
test('CSHIFTR', () => {
5+
const bits1: Bits = [1, 0, 0, 0, 1, 1, 0, 1]
6+
const expected1: Bits = [1, 1, 0, 0, 0, 1, 1, 0]
7+
expect(circularShiftRight(bits1)).toEqual(expected1)
8+
9+
const bits2: Bits = [0, 0, 0, 1, 1, 1, 1, 1]
10+
const expected2: Bits = [1, 0, 0, 0, 1, 1, 1, 1]
11+
expect(circularShiftRight(bits2)).toEqual(expected2)
12+
})

source/bits/circular-shift-right.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Bits } from '../types'
2+
3+
/**
4+
* Circular Shift Right
5+
*
6+
* @example
7+
* circularShiftRight([1,0,1,1,0,1]) => [1,1,0,1,1,0]
8+
*
9+
* @param {Array} bits input data
10+
* @return {Array} [CSHIFTR bits]
11+
*/
12+
export default (bits: Bits): Bits => {
13+
const result: Bits = []
14+
15+
result[0] = bits.pop()
16+
for (let i: number = 0; i < bits.length; i++) result[i + 1] = bits[i]
17+
18+
return result
19+
}

source/bits/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import and from './and'
2+
import circularShiftLeft from './circular-shift-left'
3+
import circularShiftRight from './circular-shift-right'
24
import nand from './nand'
35
import nor from './nor'
46
import not from './not'
@@ -16,6 +18,8 @@ import xor from './xor'
1618

1719
export {
1820
and,
21+
circularShiftLeft,
22+
circularShiftRight,
1923
nand,
2024
nor,
2125
not,
@@ -34,6 +38,8 @@ export {
3438

3539
export default {
3640
and,
41+
circularShiftLeft,
42+
circularShiftRight,
3743
nand,
3844
nor,
3945
not,

0 commit comments

Comments
 (0)