Socket
Socket
Sign inDemoInstall

convert-x-to-y-bit-array

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    convert-x-to-y-bit-array

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


Version published
Maintainers
1
Install size
6.50 kB
Created

Readme

Source

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

Import the function:

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

Use the function:

/**
 * 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

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.

Keywords

FAQs

Last updated on 20 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc