Giter VIP home page Giter VIP logo

bits's Introduction

Bits

Bits is a simple, and not very fast, but immutable bitset implementation in TypeScript

Examples

basics

// instantiation
const bits = new Bits(3, 4) // 0011
const bits2 = new Bits('0011', 4) // 0011

console.log(bits.toString()) // 0011
console.log(bits2.toString()) // 0011
console.log(bits.toNumber()) // 3
console.log(bits2.toNumber()) // 3

// get the bit at position(index)
console.log(bits.getAt(0)) // true
console.log(bits.getAt(1)) // true
console.log(bits.getAt(2)) // false
console.log(bits.getAt(3)) // false

// immutably set the value for the bit at position(index)
console.log(bits.setAt(0)(false).toString()) // 0010
console.log(bits.setAt(1)(0).toString()) // 0001
console.log(bits.setAt(2)(true).toString()) // 0111
console.log(bits.setAt(3)(1).toString()) // 1011

// immutably set the value using 'at' method
console.log(bits.at(0)(false).toString()) // 0010
console.log(bits.at(1)(0).toString()) // 0001
console.log(bits.at(2)(true).toString()) // 0111
console.log(bits.at(3)(1).toString()) // 1011

// 'at' method can also use as a 'getAt' alternative
console.log(bits.at(0)()) // true
console.log(bits.at(1)()) // true
console.log(bits.at(2)()) // false
console.log(bits.at(3)()) // false

some utility methods

const emptyBits = new Bits(0, 8) // 00000000
console.log(emptyBits.all()) // false
console.log(emptyBits.none()) // true

const notEmptyBits = emptyBits.at(2)(1) // 00000100
console.log(notEmptyBits.none()) // false
console.log(notEmptyBits.any()) // true

const whateverBits = new Bits('1010111', 8) // 01010111
console.log(whateverBits.count()) // 5 
console.log(whateverBits.flip().toString()) // 10101000

bits's People

Contributors

dependabot[bot] avatar tars0x9752 avatar

Watchers

 avatar  avatar

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.