bitwise-operation
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -1,2 +0,2 @@ | ||
// Bitwise.js 1.0.3 | ||
// Bitwise.js 1.0.4 | ||
// Bitwise may be freely distributed under the MIT license. | ||
@@ -11,8 +11,26 @@ | ||
function Bitwise(value) { | ||
if (!(this instanceof Bitwise)) return new Bitwise(value); | ||
this.value = value || 0; | ||
var self = this, | ||
idx = 0; | ||
if (!(self instanceof Bitwise)) return new Bitwise(value); | ||
if (Array.isArray(value)) { | ||
self.value = 0; | ||
value.forEach(function(value, idx) { | ||
self.value |= (value ? 1 : 0) << idx; | ||
}); | ||
} else if (typeof value == "string") { | ||
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; | ||
idx++; | ||
} | ||
} | ||
} else { | ||
self.value = value || 0; | ||
} | ||
} | ||
// Current version. | ||
Bitwise.VERSION = '1.0.3'; | ||
Bitwise.VERSION = '1.0.4'; | ||
@@ -81,6 +99,27 @@ Bitwise.and = function() { | ||
Bitwise.prototype.toString = function() { | ||
return this.value.toString(2); | ||
Bitwise.prototype.toString = function(length, separator) { | ||
var args = Array.prototype.slice.call(arguments), | ||
value = this.value.toString(2); | ||
if (args.length < 2) return value; | ||
return value.replace(new RegExp("\\B(?=(\\d{" + length + "})+(?!\\d))", "g"), separator); | ||
}; | ||
Bitwise.prototype.toArray = function() { | ||
var value = this.value, | ||
array = []; | ||
if (value == 0) return [0]; | ||
while (value > 0) { | ||
array.push(value & 1 > 0 ? 1 : 0); | ||
value = value >> 1; | ||
} | ||
return array; | ||
}; | ||
Bitwise.prototype.cardinality = function() { | ||
return this.toString().match(/(1)/g).length; | ||
}; | ||
Bitwise.prototype.length = function() { | ||
@@ -87,0 +126,0 @@ if (this.value >= 0 && Number.isSafeInteger(this.value)) return this.toString().length; |
@@ -13,2 +13,6 @@ { | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Relik77/bitwise-operation.git" | ||
}, | ||
"keywords": [ | ||
@@ -32,3 +36,3 @@ "util", | ||
"readme": "ERROR: No README data found!", | ||
"version": "1.0.3" | ||
"version": "1.0.4" | ||
} |
155
README.md
@@ -44,2 +44,4 @@ Node.js: bitwise-operation | ||
- [toString](#toString) | ||
- [toArray](#toArray) | ||
- [cardinality](#cardinality) | ||
@@ -49,4 +51,3 @@ | ||
**new Bitwise(value)** | ||
**new Bitwise(value)** | ||
**Bitwise(value)** | ||
@@ -68,2 +69,12 @@ | ||
=> 0b0110 | ||
Bitwise([0, 0, 1, 0]) | ||
.valueOf(); | ||
=> 0b0100 | ||
Bitwise("1011") | ||
.valueOf(); | ||
=> 0b1011 | ||
``` | ||
@@ -74,4 +85,3 @@ | ||
**Bitwise.and(...values)** | ||
**Bitwise.and(...values)** | ||
**Bitwise(value).and(value)** | ||
@@ -81,9 +91,9 @@ | ||
Truth table: | ||
a | b | a AND b | ||
--|--|-- | ||
0 | 0 | 0 | ||
0 | 1 | 0 | ||
1 | 0 | 0 | ||
1 | 1 | 1 | ||
Truth table: | ||
| a | b | a AND b | | ||
|---|---|---------| | ||
| 0 | 0 | 0 | | ||
| 0 | 1 | 0 | | ||
| 1 | 0 | 0 | | ||
| 1 | 1 | 1 | | ||
@@ -107,4 +117,3 @@ Example: | ||
**Bitwise.or(...values)** | ||
**Bitwise.or(...values)** | ||
**Bitwise(value).or(value)** | ||
@@ -114,9 +123,9 @@ | ||
Truth table: | ||
a | b | a OR b | ||
--|--|-- | ||
0 | 0 | 0 | ||
0 | 1 | 1 | ||
1 | 0 | 1 | ||
1 | 1 | 1 | ||
Truth table: | ||
| a | b | a OR b | | ||
|---|---|--------| | ||
| 0 | 0 | 0 | | ||
| 0 | 1 | 1 | | ||
| 1 | 0 | 1 | | ||
| 1 | 1 | 1 | | ||
@@ -140,4 +149,3 @@ Example: | ||
**Bitwise.xor(...values)** | ||
**Bitwise.xor(...values)** | ||
**Bitwise(value).xor(value)** | ||
@@ -147,9 +155,9 @@ | ||
Truth table: | ||
a | b | a XOR b | ||
--|--|-- | ||
0 | 0 | 0 | ||
0 | 1 | 1 | ||
1 | 0 | 1 | ||
1 | 1 | 0 | ||
Truth table: | ||
| a | b | a XOR b | | ||
|---|---|---------| | ||
| 0 | 0 | 0 | | ||
| 0 | 1 | 1 | | ||
| 1 | 0 | 1 | | ||
| 1 | 1 | 0 | | ||
@@ -173,4 +181,3 @@ Example: | ||
**Bitwise.nand(...values)** | ||
**Bitwise.nand(...values)** | ||
**Bitwise(value).nand(value)** | ||
@@ -180,9 +187,9 @@ | ||
Truth table: | ||
a | b | a NAND b | ||
--|--|-- | ||
0 | 0 | 1 | ||
0 | 1 | 1 | ||
1 | 0 | 1 | ||
1 | 1 | 0 | ||
Truth table: | ||
| a | b | a NAND b | | ||
|---|---|----------| | ||
| 0 | 0 | 1 | | ||
| 0 | 1 | 1 | | ||
| 1 | 0 | 1 | | ||
| 1 | 1 | 0 | | ||
@@ -211,7 +218,7 @@ Example: | ||
Truth table: | ||
a | NOT a | ||
--|-- | ||
0 | 1 | ||
1 | 0 | ||
Truth table: | ||
| a | NOT a | | ||
| --|-------| | ||
| 0 | 1 | | ||
| 1 | 0 | | ||
@@ -232,4 +239,3 @@ Example: | ||
**Bitwise.mask([fromIndex= 0,] toIndex)** | ||
**Bitwise.mask([fromIndex= 0,] toIndex)** | ||
**Bitwise(value).mask([fromIndex= 0,] toIndex)** | ||
@@ -499,5 +505,6 @@ | ||
**Bitwise(value).toString()** | ||
**Bitwise(value).toString()** | ||
**Bitwise(value).toString(length, separator)** | ||
Returns a string representation of this bit set. | ||
Returns a string representation of this Bitwise. | ||
@@ -513,3 +520,57 @@ Example: | ||
=> "0101" | ||
Bitwise(571) | ||
.toString(4, " "); | ||
=> "10 0011 1011" | ||
``` | ||
### toArray() | ||
**Bitwise(value).toArray()** | ||
Returns a array representation of this Bitwise. | ||
Example: | ||
```js | ||
var Bitwise = require('bitwise-operation') | ||
var bitwise = Bitwise(571); | ||
bitwise.toString(4, " "); | ||
=> "10 0011 1011" | ||
bitwise.toArray(); | ||
=> [ 1, 1, 0, 1, 1, 1, 0, 0, 0, 1 ] | ||
``` | ||
### cardinality() | ||
**Bitwise(value).cardinality()** | ||
Returns the number of bits set to true in this Bitwise. | ||
Example: | ||
```js | ||
var Bitwise = require('bitwise-operation') | ||
var bitwise = Bitwise(571); | ||
bitwise.toString(4, " "); | ||
=> "10 0011 1011" | ||
bitwise.cardinality(); | ||
=> 6 | ||
``` | ||
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
18633
213
562
0