Socket
Socket
Sign inDemoInstall

bitfields

Package Overview
Dependencies
2
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    bitfields

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


Version published
Maintainers
1
Install size
440 kB
Created

Readme

Source

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

FAQs

Last updated on 11 Apr 2021

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