Fork me on GitHub

autoBits.js: simple binary bitwise library

You can use it to send lots an lots of boolean values in a little package.

You can store 31 values per instance and use in a cookie, through twitter, or in a single integer column in a database.

Usage

optimusPrime = new autoBits({map: ["namedValue0", ..., "namedValue30"]});

Methods

getBits()
gets the binary value of the bits
addBit(namedValue)
sets the bit for the namedValue from the bit map
removeBit(namedValue)
removes the bit for the namedValue from the bit map
isSet(namedValue)
tests to see whether namedValue bit is set in the binary value
clear()
sets the binary value to 0, removing all bits
getBooleans()
returns an array of boolean values from the binary value

Options

bits
binary value that contains previously set bits. Default is 0
map
array of named values that correspond to the bits in the binary value. Map can be as long as 31 values.
debug
turns on console messaging. Default is false

Demos

optimusPrime = new autoBits({
	map: ["namedValue0", "namedValue1", "namedValue2", "namedValue3", "namedValue4", "namedValue5", "namedValue6", "namedValue7", "namedValue8", "namedValue9", "namedValue10", "namedValue11", "namedValue12"]
});
optimusPrime.addBit('namedValue3')
optimusPrime.addBit('namedValue12')
optimusPrime.removeBit('namedValue3')
optimusPrime.removeBit('namedValue12')
optimusPrime.isSet('namedValue3')
optimusPrime.getBits()
optimusPrime.getBooleans()

bumbleBee = new autoBits({
	map: ["namedValue0", "namedValue1", "namedValue2", "namedValue3", "namedValue4", "namedValue5", "namedValue6", "namedValue7", "namedValue8", "namedValue9", "namedValue10", "namedValue11", "namedValue12", "namedValue13", "namedValue14", "namedValue15", "namedValue16", "namedValue17", "namedValue18", "namedValue19", "namedValue20", "namedValue21", "namedValue22", "namedValue23", "namedValue24", "namedValue25", "namedValue26", "namedValue27", "namedValue28", "namedValue29", "namedValue30"],
	bits : 1073741832, //comes from DB, cookie, twitter, whatever
	debug: true //look in your console
});
bumbleBee.isSet('namedValue3')
bumbleBee.isSet('namedValue30')
bumbleBee.addBit('namedValue7')
bumbleBee.removeBit('namedValue7')
bumbleBee.isSet('namedValue7')
bumbleBee.isSet('notThere')
bumbleBee.getBits()
bumbleBee.getBooleans()

More Info

Here's an example.
Let's say you had to store a bunch of personalization data:

isBorderRedfalse
isBorderTopRedtrue
isBorderBottomRedtrue
isBackgroundGradientfalse
isBackgroundImagetrue
isBackgroundStaticfalse
isFluidtrue
isRadiustrue
isThinClientfalse
isSVGtrue
isEmbeddedtrue
isCrawlActivetrue
isFontAwesometrue
isDrunkfalse
isNewtrue
isDynamicfalse
hasChildrentrue
hasParentsfalse
hasSiblingstrue
isOpenfalse
isShortfalse
isFondOfCheesetrue
isExpiredfalse
hasUniqueIDtrue
isBlueSkyfalse
isForceEnabledtrue
hasLaserstrue
isNamedValuetrue
hasCockpittrue
isMembertrue
hasRightsfalse

Transforms into: 1051025110

Use two or three instances and suddenly you have lots of data in a few integers, so long as you have the maps.

Read more:
http://www.livephysics.com/ptools/bitwise-operation.php

autoBits.js is licensed under The MIT license

The MIT License (MIT)

Copyright (c) 2013 ResonanceMultimedia LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.