GithubHelp home page GithubHelp logo

retroscepter / bitfields Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 1.04 MB

Simple bitfields in JavaScript.

License: MIT License

TypeScript 91.31% JavaScript 6.33% Makefile 2.36%
bitfield javascript nodejs browser bitfields

bitfields's Introduction

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/[email protected]/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

bitfields's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

bitfields's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.