bit-consumer
v1.0.1
Published
An ES module providing a "bit consumer" class allowing you to feed it endless of bits and receive unsigned integers back from them whenever the wanted amount of bits has been consumed. The bits are consumed in the order of the most significant bits first.
Downloads
8
Maintainers
Readme
bit-consumer
Description
An ES module providing a "bit consumer" class allowing you to feed it endless of bits and receive unsigned integers back from them whenever the wanted amount of bits has been consumed. The bits are consumed in the order of the most significant bits first.
Example
import {BitConsumer} from 'bit-consumer'
let bc = new BitConsumer(16)
bc.onIntegerReady = integer => console.log(integer.toString(16))
bc.consumeBitsFromInteger(0xDE, 8)
bc.consumeBitsFromInteger(0xAD, 8)
bc.consumeBitsFromInteger(0xBEEF, 16)
bc.consumeBitsFromInteger(0xFF, 8)
console.log(bc.integerFromRemains().toString(16))
bc = new BitConsumer(8, integer => console.log(integer.toString(2)))
bc.consumeBits(1,1,0,0, 0,0,0,1, 0,1,0)
console.log(bc.integerFromRemains().toString(2))
Console output from example:
dead
beef
ff00
11000001
1000000
Supported platforms
- Node.js
- Deno
- A proper browser (Chromium based usually) or just Babel the shit out of it if you need legacy support.
How to use
Install using NPM
npm i bit-consumer
Import the ES module into Node.js
import {BitConsumer} from 'bit-consumer'
Got problems using ES modules? Click here or read this.
Import the ES module into the browser or Deno
import {BitConsumer} from '/node_modules/bit-consumer/source/bitConsumer.js'
Cumbersome? Consider using import maps so you can import it just like in Node.js. Also see the Deno specific documentation for import maps if using Deno.
Funding
If you find this useful then please consider helping me out (I'm jobless and sick). For more information visit my GitHub sponsors page, my profile or my simple website.
Auto-generated API documentation (from JSDoc)
bit-consumer.BitConsumer
A "bit consumer" class allowing you to feed it endless of bits and receive unsigned integers back from them whenever the wanted amount of bits has been consumed. The bits are consumed in the order of the most significant bits first.
Kind: static class of bit-consumer
new exports.BitConsumer(bitsWanted, [onIntegerReady])
Params
- bitsWanted number - Each time this many bits has been consumed the
onIntegerReady
callback is called with an integer created from those bits. - [onIntegerReady] function - The callback function to receive an integer each time the amount of wanted bits has been consumed. If more than 32 bits are wanted then it will receive a
BigInt
. It's optional because it can be set later.
bitConsumer.onIntegerReady
Set to a callback function to receive an integer each time the amount of wanted bits has been consumed. If more than 32 bits are wanted then it will receive a BigInt
.
Kind: instance property of BitConsumer
bitConsumer.integerFromRemains() ⇒ Number | BigInt
If the BitConsumer
has consumed more bits since the last call to the onIntegerReady
callback and you want to get an integer from them before the count of bits has reached bitsWanted
then call this function. It returns null
if no more bits were consumed. This is usually used to flush any remaining bits at the end of consumption.
Kind: instance method of BitConsumer
bitConsumer.consumeBitsFromInteger(integer, bitsToConsume)
The bits must come from somewhere and consuming them from an integer (up to 32 bits) is the fastest way to do it. If you need more bits you can consume them from a BigInt. If the integer has more bits than the amount you want to consume then it will consume the least significant bits of that integer.
Kind: instance method of BitConsumer
Params
- integer number | bigint - The
Number
orBigInt
to consume bits from. - bitsToConsume number - The amount of bits to consume.
bitConsumer.consumeBit(bit)
If you want to consume just one single lonely bit then this is the command for that. For higher performance consider using consumeBitsFromInteger
to consume several bits at once.
Kind: instance method of BitConsumer
Params
- bit number
bitConsumer.consumeBits(...bits)
Allows you to consume several bits. For higher performance consider using consumeBitsFromInteger
if possible.
Kind: instance method of BitConsumer
Params
- ...bits number
End of readme
To get back up click here (only works on GitHub?) or find your own way.