Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
bits operation dev and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
crisconru committed Jan 9, 2024
1 parent 723f0ec commit 891cae0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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()
}
}
})

0 comments on commit 891cae0

Please sign in to comment.