diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..9b95ab7 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,6 @@ +import { Uint16, Uint32 } from "./types"; + + +export const getUint32 = (lsb: Uint16, msb: Uint16): Uint32 => Uint32Array.from([(msb << 16) | lsb])[0] + +export const getBit = (num: number, bit: number): boolean => (num >>> bit) % 2 != 0 \ No newline at end of file diff --git a/tests/utils.test.ts b/tests/utils.test.ts new file mode 100644 index 0000000..222c06d --- /dev/null +++ b/tests/utils.test.ts @@ -0,0 +1,22 @@ +import { test, expect } from 'vitest' +import * as utils from '../src/utils' + +test('getUint32', () => { + const a = 0b1111_1010_0101_0000 + const b = 0b1010_0101_0000_1111 + const b_a = Uint32Array.from([0b1010_0101_0000_1111_1111_1010_0101_0000])[0] + const d = utils.getUint32(a, b) + expect(d).toBe(b_a) +}) + +test('getBit', () => { + const bits = 0b1010_1010_1010_1010 + for (let i = 0; i < 16; i++) { + const bit = utils.getBit(bits, i) + if ((i % 2) === 0) { + expect(bit).toBeFalsy() + } else { + expect(bit).toBeTruthy() + } + } +}) \ No newline at end of file