1.0.3 • Published 3 years ago

bitfields v1.0.3

Weekly downloads
13
License
MIT
Repository
github
Last release
3 years ago

bitfields Node CI Coverage Status

Easy-to-use bitfields for Node.js and browsers.

Installation

For Node.js or webpack projects, install with the NPM package manager:

npm install --save bitfields

For use in the browser without a bundler, include this script tag in your HTML.

<script src="https://unpkg.com/bitfields@1.0.3/dist/bitfields.js"></script>

Usage

// commonjs module
const { Bitfield } = require('bitfields')

// esmodules
import { Bitfield } from 'bitfields'

// in the browser
const { Bitfield } = window.Bitfields

const bitfield = new Bitfield(64) // create a Bitfield with 64 bits

bitfield.set(1) // set the first bit
bitfield.set(1, 1) // same as above
bitfield.set(1, true) // same as above

bitfield.set(3) // set the third bit

console.log(bitfield.get(1)) // 'true'
console.log(bitfield.get(2)) // 'false'
console.log(bitfield.get(3)) // 'true'

bitfield.set(3, false) // unset the third bit
bitfield.set(3, 0) // same as above

console.log(bitfield.get(3)) // 'false'

const buffer = bitfield.toBuffer() // convert to Buffer
const bigint = bitfield.toBigInt() // convert to BigInt
const hex = bitfield.toHex() // convert to hexadecimal string
const string = bitfield.toString() // convert to UTF-8 encoded string

console.log(buffer) // '<Buffer 40 00 00 00 00 00 00 00>'
console.log(bigint) // '4611686018427387904n'
console.log(hex) // '4000000000000000'
console.log(string) // '@'

const bitfieldFromBuffer = Bitfield.fromBuffer(buffer) // create a Bitfield from a Buffer
const bitfieldFromBigInt = Bitfield.fromBigInt(bigint) // create a Bitfield from a BigInt
const bitfieldFromHex = Bitfield.fromHex(hex) // create a Bitfield from a hexadecimal string
const bitfieldFromString = Bitfield.fromString(string) // create a Bitfield from a string

API Documentation

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago