bitwise-operation
Advanced tools
Comparing version 1.0.4 to 1.1.0
122
bitwise.js
@@ -1,2 +0,2 @@ | ||
// Bitwise.js 1.0.4 | ||
// Bitwise.js 1.1.0 | ||
// Bitwise may be freely distributed under the MIT license. | ||
@@ -11,4 +11,3 @@ | ||
function Bitwise(value) { | ||
var self = this, | ||
idx = 0; | ||
var self = this; | ||
@@ -18,10 +17,12 @@ if (!(self instanceof Bitwise)) return new Bitwise(value); | ||
self.value = 0; | ||
value.forEach(function(value, idx) { | ||
self.value |= (value ? 1 : 0) << idx; | ||
value.forEach(function(bit, idx) { | ||
self.value |= (bit ? 1 : 0) << idx; | ||
}); | ||
} else if (typeof value == "string") { | ||
var idx = 0; | ||
self.value = 0; | ||
for (var i = value.length - 1; i >= 0; i--) { | ||
if (value[i] == "1" || value[i] == "0") { | ||
self.value |= (value[i] == "1" ? 1 : 0) << idx; | ||
if (value[i] === "1" || value[i] === "0") { | ||
self.value |= (value[i] === "1" ? 1 : 0) << idx; | ||
idx++; | ||
@@ -36,4 +37,8 @@ } | ||
// Current version. | ||
Bitwise.VERSION = '1.0.4'; | ||
Bitwise.VERSION = '1.1.0'; | ||
Bitwise.not = function(value) { | ||
return ~value; | ||
}; | ||
Bitwise.and = function() { | ||
@@ -47,5 +52,17 @@ var args = Array.prototype.slice.call(arguments), | ||
} | ||
return bitwise; | ||
return bitwise.valueOf(); | ||
}; | ||
Bitwise.nand = function() { | ||
var args = Array.prototype.slice.call(arguments), | ||
bitwise = args.shift(); | ||
if (!(bitwise instanceof Bitwise)) bitwise = new Bitwise(bitwise); | ||
while (args.length > 0) { | ||
bitwise.nand(args.shift()); | ||
} | ||
return bitwise.valueOf(); | ||
}; | ||
Bitwise.andNot = Bitwise.nand; | ||
Bitwise.or = function() { | ||
@@ -59,5 +76,16 @@ var args = Array.prototype.slice.call(arguments), | ||
} | ||
return bitwise; | ||
return bitwise.valueOf(); | ||
}; | ||
Bitwise.nor = function() { | ||
var args = Array.prototype.slice.call(arguments), | ||
bitwise = args.shift(); | ||
if (!(bitwise instanceof Bitwise)) bitwise = new Bitwise(bitwise); | ||
while (args.length > 0) { | ||
bitwise.nor(args.shift()); | ||
} | ||
return bitwise.valueOf(); | ||
}; | ||
Bitwise.xor = function() { | ||
@@ -71,6 +99,6 @@ var args = Array.prototype.slice.call(arguments), | ||
} | ||
return bitwise; | ||
return bitwise.valueOf(); | ||
}; | ||
Bitwise.nand = function() { | ||
Bitwise.xnor = function() { | ||
var args = Array.prototype.slice.call(arguments), | ||
@@ -81,7 +109,30 @@ bitwise = args.shift(); | ||
while (args.length > 0) { | ||
bitwise.nand(args.shift()); | ||
bitwise.xnor(args.shift()); | ||
} | ||
return bitwise; | ||
return bitwise.valueOf(); | ||
}; | ||
Bitwise.nxor = Bitwise.xnor; | ||
Bitwise.toggle = function() { | ||
var args = Array.prototype.slice.call(arguments), | ||
bitwise = args.shift(); | ||
if (!(bitwise instanceof Bitwise)) bitwise = new Bitwise(bitwise); | ||
while (args.length > 0) { | ||
bitwise.toggle(args.shift()); | ||
} | ||
return bitwise.valueOf(); | ||
}; | ||
Bitwise.swap = function() { | ||
var args = Array.prototype.slice.call(arguments), | ||
bitwise = args.shift(); | ||
if (!(bitwise instanceof Bitwise)) bitwise = new Bitwise(bitwise); | ||
while (args.length > 0) { | ||
bitwise.swap(args.shift()); | ||
} | ||
return bitwise.valueOf(); | ||
}; | ||
Bitwise.mask = function(from, to) { | ||
@@ -92,3 +143,3 @@ var args = Array.prototype.slice.call(arguments); | ||
from = args.pop() || 0; | ||
return Bitwise().setRange(from, to); | ||
return Bitwise(0).setRange(from, to).valueOf(); | ||
}; | ||
@@ -118,3 +169,3 @@ | ||
if (value == 0) return [0]; | ||
if (value === 0) return [0]; | ||
@@ -138,2 +189,3 @@ while (value > 0) { | ||
Bitwise.prototype.not = function() { | ||
@@ -150,2 +202,8 @@ this.value = ~this.value; | ||
Bitwise.prototype.nand = function(value) { | ||
this.and(value).not(); | ||
return this; | ||
}; | ||
Bitwise.prototype.andNot = Bitwise.prototype.nand; | ||
Bitwise.prototype.or = function(value) { | ||
@@ -157,2 +215,7 @@ if (value instanceof Bitwise) value = value.valueOf(); | ||
Bitwise.prototype.nor = function(value) { | ||
this.or(value).not(); | ||
return this; | ||
}; | ||
Bitwise.prototype.xor = function(value) { | ||
@@ -163,12 +226,13 @@ this.value = this.copy().or(value).and(this.copy().and(value).not()).valueOf(); | ||
Bitwise.prototype.nand = function(value) { | ||
if (!(value instanceof Bitwise)) value = new Bitwise(value); | ||
this.and(value).not(); | ||
Bitwise.prototype.xnor = function(value) { | ||
this.xor(value).not(); | ||
return this; | ||
}; | ||
Bitwise.prototype.andNot = Bitwise.prototype.nand; | ||
Bitwise.prototype.nxor = Bitwise.prototype.xnor; | ||
Bitwise.prototype.set = function(idx, value) { | ||
if (value === false) return this.unset(idx); | ||
if (idx < 0) return; | ||
if (idx < 0) return this; | ||
@@ -199,2 +263,6 @@ this.value |= 1 << idx; | ||
if (idx1 < 0 || idx2 < 0) return this; | ||
if (Array.isArray(idx1)) { | ||
idx2 = idx1.pop(); | ||
idx1 = idx1.shift(); | ||
} | ||
@@ -210,3 +278,3 @@ var tmp = this.copy(); | ||
if (value instanceof Bitwise) value = value.valueOf(); | ||
return (this.value & value) == value; | ||
return this.value === value; | ||
}; | ||
@@ -223,3 +291,3 @@ | ||
this.value |= Bitwise.xor((1 << from) - 1, (1 << to << 1) - 1).valueOf(); | ||
this.value |= Bitwise.xor((1 << from) - 1, (1 << to << 1) - 1); | ||
return this; | ||
@@ -231,3 +299,3 @@ }; | ||
this.value &= Bitwise.xor((1 << from) - 1, (1 << to << 1) - 1).not().valueOf(); | ||
this.value &= ~Bitwise.xor((1 << from) - 1, (1 << to << 1) - 1); | ||
return this; | ||
@@ -241,3 +309,3 @@ }; | ||
this.value = this.copy().not().and(mask).or(this.copy().and(mask.copy().not())).valueOf(); | ||
this.value = this.copy().not().and(mask).or(this.copy().and(Bitwise.not(mask))).valueOf(); | ||
return this; | ||
@@ -247,3 +315,3 @@ }; | ||
Bitwise.prototype.mask = function(from, to) { | ||
this.and(Bitwise.mask.apply(Bitwise, arguments)); | ||
this.and(Bitwise.mask(from, to)); | ||
return this; | ||
@@ -253,3 +321,3 @@ }; | ||
Bitwise.prototype.clear = function(from, to) { | ||
this.and(Bitwise.mask.apply(Bitwise, arguments).not()); | ||
this.and(Bitwise.not(Bitwise.mask(from, to))); | ||
return this; | ||
@@ -256,0 +324,0 @@ }; |
@@ -6,2 +6,16 @@ { | ||
}, | ||
"devDependencies": { | ||
"eslint": "1.10.x", | ||
"qunit-cli": "~0.2.0", | ||
"qunitjs": "^1.18.0" | ||
}, | ||
"scripts": { | ||
"test": "npm run test-node && npm run lint", | ||
"coverage": "nyc npm run test-node && nyc report", | ||
"coveralls": "nyc npm run test-node && nyc report --reporter=text-lcov | coveralls", | ||
"lint": "eslint bitwise.js", | ||
"test-node": "qunit-cli test/tests.js", | ||
"minify": "uglifyjs bitwise.js -c \"evaluate=false\" --comments \"/ .*/\" -m", | ||
"build": "npm run minify -- --source-map bitwise-min.map --source-map-url \" \" -o bitwise-min.js" | ||
}, | ||
"dependencies": {}, | ||
@@ -12,2 +26,4 @@ "description": "JavaScript's bitwise operation helper library.", | ||
"bitwise.js", | ||
"bitwise-min.js", | ||
"bitwise-min.map", | ||
"LICENSE" | ||
@@ -37,3 +53,3 @@ ], | ||
"readme": "ERROR: No README data found!", | ||
"version": "1.0.4" | ||
"version": "1.1.0" | ||
} |
207
README.md
@@ -22,8 +22,10 @@ Node.js: bitwise-operation | ||
------- | ||
- [Bitwise](#Bitwise) | ||
- [Bitwise](#bitwise) | ||
- [not](#not) | ||
- [and](#and) | ||
- [nand](#nand) | ||
- [or](#or) | ||
- [nor](#nor) | ||
- [xor](#xor) | ||
- [nand](#nand) | ||
- [not](#not) | ||
- [xnor](#xnor) | ||
- [mask](#mask) | ||
@@ -38,10 +40,10 @@ - [clear](#mask) | ||
- [equals](#equals) | ||
- [setValue](#setValue) | ||
- [setRange](#setRange) | ||
- [unsetRange](#unsetRange) | ||
- [toggleRange](#toggleRange) | ||
- [setValue](#setvalue) | ||
- [setRange](#setrange) | ||
- [unsetRange](#unsetrange) | ||
- [toggleRange](#togglerange) | ||
- [copy](#copy) | ||
- [valueOf](#valueOf) | ||
- [toString](#toString) | ||
- [toArray](#toArray) | ||
- [valueOf](#valueof) | ||
- [toString](#tostring) | ||
- [toArray](#toarray) | ||
- [cardinality](#cardinality) | ||
@@ -81,3 +83,29 @@ | ||
### not() | ||
**Bitwise.not(value)** | ||
**Bitwise(value).not()** | ||
Performs a logical **NOT** of this target bit set. | ||
Truth table: | ||
| a | NOT a | | ||
| --|-------| | ||
| 0 | 1 | | ||
| 1 | 0 | | ||
Example: | ||
```js | ||
var Bitwise = require('bitwise-operation') | ||
Bitwise.not(0b1010); | ||
Bitwise(0b1010) | ||
.not() | ||
.valueOf(); | ||
=> 0b0101 | ||
``` | ||
### and() | ||
@@ -103,3 +131,3 @@ | ||
Bitwise.and(0b0111, Bitwise(0b0101), 0b1100) | ||
Bitwise.and(0b0111, Bitwise(0b0101), 0b1100); | ||
@@ -114,2 +142,32 @@ Bitwise(0b0111) | ||
### nand() | ||
**Bitwise.nand(...values)** | ||
**Bitwise(value).nand(value)** | ||
Clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet. | ||
Truth table: | ||
| a | b | a NAND b | | ||
|---|---|----------| | ||
| 0 | 0 | 1 | | ||
| 0 | 1 | 1 | | ||
| 1 | 0 | 1 | | ||
| 1 | 1 | 0 | | ||
Example: | ||
```js | ||
var Bitwise = require('bitwise-operation') | ||
Bitwise.nand(0b0010, 0b0110); | ||
Bitwise(0b0010) | ||
.nand(0b0110) | ||
.valueOf(); | ||
=> 0b1101 | ||
``` | ||
Alias: **andNot()** | ||
### or() | ||
@@ -135,4 +193,3 @@ | ||
Bitwise.or(0b0010, 0b0110) | ||
.value(); | ||
Bitwise.or(0b0010, 0b0110); | ||
@@ -144,2 +201,30 @@ Bitwise(0b0010) | ||
=> 0b0110 | ||
### nor() | ||
**Bitwise.nor(...values)** | ||
**Bitwise(value).nor(value)** | ||
Performs a logical **NOR** of this bit set with the bit set argument. | ||
Truth table: | ||
| a | b | a NOR b | | ||
|---|---|---------| | ||
| 0 | 0 | 1 | | ||
| 0 | 1 | 0 | | ||
| 1 | 0 | 0 | | ||
| 1 | 1 | 0 | | ||
Example: | ||
```js | ||
var Bitwise = require('bitwise-operation') | ||
Bitwise.nor(0b0010, 0b0110); | ||
Bitwise(0b0010) | ||
.nor(0b0110) | ||
.valueOf(); | ||
=> 0b1001 | ||
``` | ||
@@ -167,4 +252,3 @@ | ||
Bitwise.xor(0b0010, 0b0110) | ||
.value(); | ||
Bitwise.xor(0b0010, 0b0110); | ||
@@ -178,16 +262,16 @@ Bitwise(0b0010) | ||
### nand() | ||
### xnor() | ||
**Bitwise.nand(...values)** | ||
**Bitwise(value).nand(value)** | ||
**Bitwise.xnor(...values)** | ||
**Bitwise(value).xnor(value)** | ||
Clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet. | ||
Performs a logical **XNOR** of this bit set with the bit set argument. | ||
Truth table: | ||
| a | b | a NAND b | | ||
|---|---|----------| | ||
| 0 | 0 | 1 | | ||
| 0 | 1 | 1 | | ||
| 1 | 0 | 1 | | ||
| 1 | 1 | 0 | | ||
Truth table: | ||
| a | b | a XNOR b | | ||
|---|---|----------| | ||
| 0 | 0 | 1 | | ||
| 0 | 1 | 0 | | ||
| 1 | 0 | 0 | | ||
| 1 | 1 | 1 | | ||
@@ -199,37 +283,12 @@ Example: | ||
Bitwise.nand(0b0010, 0b0110) | ||
.value(); | ||
Bitwise.xor(0b0010, 0b0110); | ||
Bitwise(0b0010) | ||
.nand(0b0110) | ||
.valueOf(); | ||
.xor(0b0110) | ||
.valueOf(); | ||
=> 0b1101 | ||
=> 0b1011 | ||
``` | ||
Alias: **andNot()** | ||
Alias: **nxor()** | ||
### not() | ||
**Bitwise(value).not()** | ||
Performs a logical **NOT** of this target bit set. | ||
Truth table: | ||
| a | NOT a | | ||
| --|-------| | ||
| 0 | 1 | | ||
| 1 | 0 | | ||
Example: | ||
```js | ||
var Bitwise = require('bitwise-operation') | ||
Bitwise(0b1010) | ||
.not() | ||
.valueOf(); | ||
=> 0b0101 | ||
``` | ||
### mask() | ||
@@ -247,5 +306,8 @@ | ||
Bitwise.mask(1, 5); // => 0b00011110 | ||
Bitwise(0b1011011) | ||
.mask(1, 5) // => 0b00011110 | ||
.valueOf(); | ||
// Equal to: Bitwise(0b1011011).and(Bitwise.mask(1, 5)).valueOf() | ||
@@ -347,2 +409,3 @@ => 0b00011010 | ||
**Bitwise.toggle(value, ...idx)** | ||
**Bitwise(value).toggle(idx)** | ||
@@ -357,2 +420,4 @@ | ||
Bitwise.toggle(0b0001, 1); | ||
Bitwise(0b0001) | ||
@@ -367,2 +432,3 @@ .toggle(1) | ||
**Bitwise.toggle(value, ...[idx1, idx2])** | ||
**Bitwise(value).swap(idx1, idx2)** | ||
@@ -377,2 +443,4 @@ | ||
Bitwise.swap(0b0101, [1, 2]); | ||
Bitwise(0b0101) | ||
@@ -414,3 +482,4 @@ .swap(1, 2) | ||
Bitwise(0b0101) | ||
.setValue(0b0111); | ||
.setValue(0b0111) | ||
.valueOf(); | ||
@@ -432,3 +501,4 @@ => 0b0111 | ||
Bitwise(0b0101) | ||
.setRange(1, 2); | ||
.setRange(1, 2) | ||
.valueOf(); | ||
@@ -450,3 +520,4 @@ => 0b0111 | ||
Bitwise(0b0101) | ||
.unsetRange(1, 2); | ||
.unsetRange(1, 2) | ||
.valueOf(); | ||
@@ -468,3 +539,4 @@ => 0b0001 | ||
Bitwise(0b0101) | ||
.toggleRange(1, 2); | ||
.toggleRange(1, 2) | ||
.valueOf(); | ||
@@ -485,6 +557,11 @@ => 0b0011 | ||
Bitwise(0b0101) | ||
.toggleRange(1, 2); | ||
var a = Bitwise(0b0001); | ||
var b = a; | ||
var c = a.copy(); | ||
=> 0b0011 | ||
a.toggle(1); | ||
a.valueOf(); // => 0b0011 | ||
b.valueOf(); // => 0b0011 | ||
c.valueOf(); // => 0b0001 | ||
``` | ||
@@ -578,5 +655,1 @@ Alias: **clone()** | ||
``` | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33021
6
290
635
3