Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thebowja/convert-x-to-y-bit-array

Converts an array of X-bit unsigned integers to an array of Y-bit unsigned integers.
https://github.com/thebowja/convert-x-to-y-bit-array

Last synced: 29 days ago
JSON representation

Converts an array of X-bit unsigned integers to an array of Y-bit unsigned integers.

Awesome Lists containing this project

README

        

# convert X to Y bit array
Converts an array of X-bit unsigned integers to an array of Y-bit unsigned integers.

Limitations: Javascript bitwise operations work within 32 bits.

## Usage

Install the npm package with:

```cmd
npm install convert-x-to-y-bit-array
```

Import the function:
```js
const convertXtoYbitarray = require("convert-x-to-y-bit-array");
```

Use the function:
```js
/**
* Converts an array of X-bit unsigned integers to an array of Y-bit unsigned integers.
*
* @param {integer} Xbit - Number of bits each integer in the input array has
* @param {integer} Ybit - Number of bits each integer in the output array should have
* @param {integer[]} inputArray - Array of X-bit unsigned integers to convert
* @param {integer} [maxOutputLength] - Optional param to limit length of output array
*
* @return {integer[]} array of Y-bit unsigned integers
*/
convertXtoYbitarray(Xbit, Ybit, inputArray[, maxOutputLength])
```

## Example

```js
const convertXtoYbitarray = require("convert-x-to-y-bit-array");

// Converts an array of 8-bit integers to an array of 12-bit integers
let result = convertXtoYbitarray(8, 12, [0, 1, 4, 16, 64]);

console.log(result);
// [ 0, 260, 260, 0 ]
```

## Visual explanation

`[0, 1, 4, 16, 64]` represented as an array of 8-bit unsigned integers is:

```
00000000 00000001 00000100 00010000 01000000
```

When converting to an array of 12-bit unsigned integers, simply group the bits into groups of 12 (filling in the end with 0s):

```
000000000000 000100000100 000100000100 000000000000
```

Which is equivalent to `[0, 260, 260, 0]`

## Why?

There are crazy people out there using 12-bit integers.

## Contributing

Create an issue or PR.