extra-bit.web 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +76 -0
- package/index.d.ts +134 -0
- package/index.js +1 -0
- package/index.mjs +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-22 Subhajit Sahu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
The [bit] is a basic unit of information in information theory, computing.
|
|
2
|
+
This package includes [bit twiddling hacks] by Sean Eron Anderson and many others.<br>
|
|
3
|
+
:package: [NPM](https://www.npmjs.com/package/extra-bit),
|
|
4
|
+
:smiley_cat: [GitHub](https://github.com/orgs/nodef/packages?repo_name=extra-bit),
|
|
5
|
+
:running: [RunKit](https://npm.runkit.com/extra-bit),
|
|
6
|
+
:vhs: [Asciinema](https://asciinema.org/a/337114),
|
|
7
|
+
:moon: [Minified](https://www.npmjs.com/package/extra-bit.min),
|
|
8
|
+
:scroll: [Files](https://unpkg.com/extra-bit.web/),
|
|
9
|
+
:newspaper: [JSDoc](https://nodef.github.io/extra-bit/),
|
|
10
|
+
:blue_book: [Wiki](https://github.com/nodef/extra-bit/wiki/).
|
|
11
|
+
|
|
12
|
+
> Stability: Experimental.
|
|
13
|
+
|
|
14
|
+
<br>
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
const bit = require("extra-bit");
|
|
18
|
+
// import * as bit from "extra-bit";
|
|
19
|
+
// import * as bit from "https://unpkg.com/extra-bit@1.0.50/index.mjs"; (deno)
|
|
20
|
+
|
|
21
|
+
bit.count(7);
|
|
22
|
+
// 3 (111 ⇒ 3)
|
|
23
|
+
|
|
24
|
+
bit.parity(8, 2);
|
|
25
|
+
// 2 (10,00 ⇒ 10)
|
|
26
|
+
|
|
27
|
+
bit.swap(6, 1, 0);
|
|
28
|
+
// 5 (110 ⇒ 101)
|
|
29
|
+
|
|
30
|
+
bit.reverse(0xFFFF0000);
|
|
31
|
+
// 65535 (0x0000FFFF)
|
|
32
|
+
|
|
33
|
+
bit.signExtend(15, 4);
|
|
34
|
+
// -1
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
<br>
|
|
38
|
+
<br>
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
## Index
|
|
42
|
+
|
|
43
|
+
| Method | Action |
|
|
44
|
+
| ------------ | ---------------------------------------- |
|
|
45
|
+
| [get] | Gets a bit. |
|
|
46
|
+
| [set] | Sets a bit. |
|
|
47
|
+
| [toggle] | Toggles a bit. |
|
|
48
|
+
| [swap] | Swaps bit sequences. |
|
|
49
|
+
| [scan] | Finds index of first set bit from LSB. |
|
|
50
|
+
| [count] | Counts bits set. |
|
|
51
|
+
| [parity] | Finds n-bit parity. |
|
|
52
|
+
| [rotate] | Rotates bits. |
|
|
53
|
+
| [reverse] | Reverses all bits. |
|
|
54
|
+
| [merge] | Merges bits as per mask. |
|
|
55
|
+
| [interleave] | Interleaves bits of two int16s. |
|
|
56
|
+
| [signExtend] | Sign extends variable bit-width integer. |
|
|
57
|
+
|
|
58
|
+
<br>
|
|
59
|
+
<br>
|
|
60
|
+
|
|
61
|
+
[](https://www.youtube.com/watch?v=4Yy0pPTrHlk)
|
|
62
|
+
|
|
63
|
+
[bit]: https://en.wikipedia.org/wiki/Bit
|
|
64
|
+
[bit twiddling hacks]: https://graphics.stanford.edu/~seander/bithacks.html
|
|
65
|
+
[get]: https://github.com/nodef/extra-bit/wiki/get
|
|
66
|
+
[set]: https://github.com/nodef/extra-bit/wiki/set
|
|
67
|
+
[swap]: https://github.com/nodef/extra-bit/wiki/swap
|
|
68
|
+
[scan]: https://github.com/nodef/extra-bit/wiki/scan
|
|
69
|
+
[count]: https://github.com/nodef/extra-bit/wiki/count
|
|
70
|
+
[parity]: https://github.com/nodef/extra-bit/wiki/parity
|
|
71
|
+
[reverse]: https://github.com/nodef/extra-bit/wiki/reverse
|
|
72
|
+
[merge]: https://github.com/nodef/extra-bit/wiki/merge
|
|
73
|
+
[interleave]: https://github.com/nodef/extra-bit/wiki/interleave
|
|
74
|
+
[signExtend]: https://github.com/nodef/extra-bit/wiki/signExtend
|
|
75
|
+
[toggle]: https://github.com/nodef/extra-bit/wiki/toggle
|
|
76
|
+
[rotate]: https://github.com/nodef/extra-bit/wiki/rotate
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get a bit.
|
|
3
|
+
* @param x an int32
|
|
4
|
+
* @param i bit index
|
|
5
|
+
* @param w bit width (1)
|
|
6
|
+
*/
|
|
7
|
+
declare function get(x: number, i: number, w?: number): number;
|
|
8
|
+
//# sourceMappingURL=get.d.ts.map
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Get bits as per mask.
|
|
12
|
+
* @param x an int32
|
|
13
|
+
* @param m bit mask
|
|
14
|
+
*/
|
|
15
|
+
declare function getAs(x: number, m: number): number;
|
|
16
|
+
//# sourceMappingURL=getAs.d.ts.map
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Set a bit.
|
|
20
|
+
* @param x an int32
|
|
21
|
+
* @param i bit index
|
|
22
|
+
* @param v bit value (1)
|
|
23
|
+
* @param w bit width (1)
|
|
24
|
+
*/
|
|
25
|
+
declare function set(x: number, i: number, v?: number, w?: number): number;
|
|
26
|
+
//# sourceMappingURL=set.d.ts.map
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Set bits as per mask.
|
|
30
|
+
* @param x an int32
|
|
31
|
+
* @param m bit mask
|
|
32
|
+
* @param f bit value (1)
|
|
33
|
+
*/
|
|
34
|
+
declare function setAs(x: number, m: number, f?: number): number;
|
|
35
|
+
//# sourceMappingURL=setAs.d.ts.map
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Toggle a bit.
|
|
39
|
+
* @param x an int32
|
|
40
|
+
* @param i bit index
|
|
41
|
+
* @param w bit width (1)
|
|
42
|
+
*/
|
|
43
|
+
declare function toggle(x: number, i: number, w?: number): number;
|
|
44
|
+
//# sourceMappingURL=toggle.d.ts.map
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Toggle bits as per mask.
|
|
48
|
+
* @param x an int32
|
|
49
|
+
* @param m bit mask
|
|
50
|
+
*/
|
|
51
|
+
declare function toggleAs(x: number, m: number): number;
|
|
52
|
+
//# sourceMappingURL=toggleAs.d.ts.map
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Swap bit sequences.
|
|
56
|
+
* @param x an int32
|
|
57
|
+
* @param i first bit index
|
|
58
|
+
* @param j second bit index
|
|
59
|
+
* @param w bit width (1)
|
|
60
|
+
*/
|
|
61
|
+
declare function swap(x: number, i: number, j: number, w?: number): number;
|
|
62
|
+
//# sourceMappingURL=swap.d.ts.map
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Find index of first set bit from LSB.
|
|
66
|
+
* @param x an int32
|
|
67
|
+
*/
|
|
68
|
+
declare function scan(x: number): number;
|
|
69
|
+
//# sourceMappingURL=scan.d.ts.map
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Find index of first set bit from MSB.
|
|
73
|
+
* @param x an int32
|
|
74
|
+
*/
|
|
75
|
+
declare function scanReverse(x: number): number;
|
|
76
|
+
//# sourceMappingURL=scanReverse.d.ts.map
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Count bits set.
|
|
80
|
+
* @param x an int32
|
|
81
|
+
*/
|
|
82
|
+
declare function count(x: number): number;
|
|
83
|
+
//# sourceMappingURL=count.d.ts.map
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Find n-bit parity.
|
|
87
|
+
* @param x an int32
|
|
88
|
+
* @param n number of bits (1)
|
|
89
|
+
*/
|
|
90
|
+
declare function parity(x: number, n?: number): number;
|
|
91
|
+
//# sourceMappingURL=parity.d.ts.map
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Rotate bits.
|
|
95
|
+
* @param x an int32
|
|
96
|
+
* @param n rotate amount (+ve: left, -ve: right)
|
|
97
|
+
*/
|
|
98
|
+
declare function rotate(x: number, n?: number): number;
|
|
99
|
+
//# sourceMappingURL=rotate.d.ts.map
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Reverse all bits.
|
|
103
|
+
* @param x an int32
|
|
104
|
+
*/
|
|
105
|
+
declare function reverse(x: number): number;
|
|
106
|
+
//# sourceMappingURL=reverse.d.ts.map
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Merge bits as per mask.
|
|
110
|
+
* @param x first int32
|
|
111
|
+
* @param y second int32
|
|
112
|
+
* @param m bit mask (0 ⇒ from x)
|
|
113
|
+
*/
|
|
114
|
+
declare function merge(x: number, y: number, m: number): number;
|
|
115
|
+
//# sourceMappingURL=merge.d.ts.map
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Interleave bits of two int16s.
|
|
119
|
+
* @param x first int16
|
|
120
|
+
* @param y second int16
|
|
121
|
+
* @returns int32
|
|
122
|
+
*/
|
|
123
|
+
declare function interleave(x: number, y: number): number;
|
|
124
|
+
//# sourceMappingURL=interleave.d.ts.map
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Sign extend variable bit-width integer.
|
|
128
|
+
* @param x variable bit-width integer
|
|
129
|
+
* @param w bit width (32)
|
|
130
|
+
*/
|
|
131
|
+
declare function signExtend(x: number, w?: number): number;
|
|
132
|
+
//# sourceMappingURL=signExtend.d.ts.map
|
|
133
|
+
|
|
134
|
+
export { count, get, getAs, interleave, merge, parity, reverse, rotate, scan, scanReverse, set, setAs, signExtend, swap, toggle, toggleAs };
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(n){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=n();else if("function"==typeof define&&define.amd)define([],n);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).extra_bit=n()}}((function(){return function n(e,r,t){function u(f,i){if(!r[f]){if(!e[f]){var c="function"==typeof require&&require;if(!i&&c)return c(f,!0);if(o)return o(f,!0);var s=new Error("Cannot find module '"+f+"'");throw s.code="MODULE_NOT_FOUND",s}var d=r[f]={exports:{}};e[f][0].call(d.exports,(function(n){return u(e[f][1][n]||n)}),d,d.exports,n,e,r,t)}return r[f].exports}for(var o="function"==typeof require&&require,f=0;f<t.length;f++)u(t[f]);return u}({1:[function(n,e,r){"use strict";function t(n){return(1<<n)-1}Object.defineProperty(r,"__esModule",{value:!0});var u=[32,0,1,26,2,23,27,0,3,16,24,30,28,11,0,13,4,7,17,0,25,22,31,15,29,10,12,6,0,21,14,9,5,20,8,19,18];var o=[0,9,1,10,13,21,2,29,11,14,16,18,22,25,3,30,8,12,20,28,15,17,24,7,19,27,23,6,26,5,4,31];r.count=function(n){return 16843009*((n=(858993459&(n-=n>>>1&1431655765))+(n>>>2&858993459))+(n>>>4)&252645135)>>>24},r.get=function(n,e,r=1){return n>>>e&t(r)},r.getAs=function(n,e){return n&e},r.interleave=function(n,e){return(e=1431655765&((e=858993459&((e=252645135&((e=16711935&(e|e<<8))|e<<4))|e<<2))|e<<1))|(n=1431655765&((n=858993459&((n=252645135&((n=16711935&(n|n<<8))|n<<4))|n<<2))|n<<1))<<1},r.merge=function(n,e,r){return n^(n^e)&r},r.parity=function(n,e=1){if(1===e)return function(n){return n^=n>>>16,n^=n>>>8,n^=n>>>4,27030>>>(n&=15)&1}(n);for(var r=(1<<e)-1,t=0;n;)t^=n&r,n>>>=e;return t},r.reverse=function(n){return(n=(n=(n=(n=n>>>1&1431655765|(1431655765&n)<<1)>>>2&858993459|(858993459&n)<<2)>>>4&252645135|(252645135&n)<<4)>>>8&16711935|(16711935&n)<<8)>>>16|n<<16},r.rotate=function(n,e=0){return e<0?n<<32+e|n>>>-e:n<<e|n>>32-e},r.scan=function(n){return u[(-n&n)%37]},r.scanReverse=function(n){return n|=n>>>1,n|=n>>>2,n|=n>>>4,n|=n>>>8,o[130329821*(n|=n>>>16)>>>27]},r.set=function(n,e,r=1,u=1){return n&~(t(u)<<e)|r<<e},r.setAs=function(n,e,r=1){return n&~e|-r&e},r.signExtend=function(n,e=32){return n<<(e=32-e)>>e},r.swap=function(n,e,r,u=1){var o=(n>>>e^n>>>r)&t(u);return n^(o<<e|o<<r)},r.toggle=function(n,e,r=1){return n^t(r)<<e},r.toggleAs=function(n,e){return n^e}},{}]},{},[1])(1)}));
|
package/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function ones(n){return(1<<n)-1}function get(n,t,e=1){return n>>>t&ones(e)}function getAs(n,t){return n&t}function set(n,t,e=1,r=1){return n&~(ones(r)<<t)|e<<t}function setAs(n,t,e=1){return n&~t|-e&t}function toggle(n,t,e=1){return n^ones(e)<<t}function toggleAs(n,t){return n^t}function swap(n,t,e,r=1){var u=(n>>>t^n>>>e)&ones(r);return n^(u<<t|u<<e)}const MOD37_POS32=[32,0,1,26,2,23,27,0,3,16,24,30,28,11,0,13,4,7,17,0,25,22,31,15,29,10,12,6,0,21,14,9,5,20,8,19,18];var MOD37_POS32$1=MOD37_POS32;function scan(n){return MOD37_POS32$1[(-n&n)%37]}const DEBRUIJN_POS32=[0,9,1,10,13,21,2,29,11,14,16,18,22,25,3,30,8,12,20,28,15,17,24,7,19,27,23,6,26,5,4,31];var DEBRUIJN_POS32$1=DEBRUIJN_POS32;function scanReverse(n){return n|=n>>>1,n|=n>>>2,n|=n>>>4,n|=n>>>8,DEBRUIJN_POS32$1[130329821*(n|=n>>>16)>>>27]}function count(n){return 16843009*((n=(858993459&(n-=n>>>1&1431655765))+(n>>>2&858993459))+(n>>>4)&252645135)>>>24}function parity1(n){return n^=n>>>16,n^=n>>>8,n^=n>>>4,27030>>>(n&=15)&1}function parity(n,t=1){if(1===t)return parity1(n);for(var e=(1<<t)-1,r=0;n;)r^=n&e,n>>>=t;return r}function rotate(n,t=0){return t<0?n<<32+t|n>>>-t:n<<t|n>>32-t}function reverse(n){return(n=(n=(n=(n=n>>>1&1431655765|(1431655765&n)<<1)>>>2&858993459|(858993459&n)<<2)>>>4&252645135|(252645135&n)<<4)>>>8&16711935|(16711935&n)<<8)>>>16|n<<16}function merge(n,t,e){return n^(n^t)&e}function interleave(n,t){return(t=1431655765&((t=858993459&((t=252645135&((t=16711935&(t|t<<8))|t<<4))|t<<2))|t<<1))|(n=1431655765&((n=858993459&((n=252645135&((n=16711935&(n|n<<8))|n<<4))|n<<2))|n<<1))<<1}function signExtend(n,t=32){return n<<(t=32-t)>>t}export{count,get,getAs,interleave,merge,parity,reverse,rotate,scan,scanReverse,set,setAs,signExtend,swap,toggle,toggleAs};
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "extra-bit.web",
|
|
3
|
+
"version": "2.0.2",
|
|
4
|
+
"description": "The bit is a basic unit of information in information theory, computing.",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "index.mjs",
|
|
8
|
+
"exports": {
|
|
9
|
+
"require": "./index.js",
|
|
10
|
+
"import": "./index.mjs"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "exit",
|
|
14
|
+
"build": "node ./build",
|
|
15
|
+
"deploy": "node ./build deploy"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"extra",
|
|
19
|
+
"bit",
|
|
20
|
+
"count",
|
|
21
|
+
"get",
|
|
22
|
+
"getAs",
|
|
23
|
+
"interleave",
|
|
24
|
+
"merge",
|
|
25
|
+
"parity",
|
|
26
|
+
"reverse",
|
|
27
|
+
"rotate",
|
|
28
|
+
"scan",
|
|
29
|
+
"scanReverse",
|
|
30
|
+
"set",
|
|
31
|
+
"setAs",
|
|
32
|
+
"signExtend",
|
|
33
|
+
"swap",
|
|
34
|
+
"toggle",
|
|
35
|
+
"toggleAs"
|
|
36
|
+
],
|
|
37
|
+
"author": "wolfram77@gmail.com",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/nodef/extra-bit.git"
|
|
42
|
+
},
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/nodef/extra-bit/issues"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/nodef/extra-bit#readme",
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@rollup/plugin-commonjs": "^21.0.3",
|
|
49
|
+
"@rollup/plugin-node-resolve": "^13.2.0",
|
|
50
|
+
"@types/node": "^17.0.23",
|
|
51
|
+
"extra-build": "^2.0.31",
|
|
52
|
+
"rollup-plugin-cleanup": "^3.2.1",
|
|
53
|
+
"rollup-plugin-dts": "^4.2.1"
|
|
54
|
+
}
|
|
55
|
+
}
|