extra-bit 2.0.2 → 2.1.1

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/README.md CHANGED
@@ -1,20 +1,23 @@
1
1
  The [bit] is a basic unit of information in information theory, computing.
2
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/),
9
- :newspaper: [JSDoc](https://nodef.github.io/extra-bit/),
10
- :blue_book: [Wiki](https://github.com/nodef/extra-bit/wiki/).
3
+ 📦 [Node.js](https://www.npmjs.com/package/extra-bit),
4
+ 🌐 [Web](https://www.npmjs.com/package/extra-bit.web),
5
+ 📜 [Files](https://unpkg.com/extra-bit/),
6
+ 📰 [Docs](https://nodef.github.io/extra-bit/).
7
+ 📘 [Wiki](https://github.com/nodef/extra-bit/wiki/).
11
8
 
12
- > Stability: Experimental.
9
+ This package is available in *Node.js* and *Web* formats. The web format
10
+ is exposed as `extra_bit` standalone variable and can be loaded from
11
+ [jsDelivr CDN].
12
+
13
+ > Stability: [Experimental](https://www.youtube.com/watch?v=L1j93RnIxEo).
14
+
15
+ [jsDelivr CDN]: https://cdn.jsdelivr.net/npm/extra-bit.web/index.js
13
16
 
14
17
  <br>
15
18
 
16
19
  ```javascript
17
- const bit = require("extra-bit");
20
+ const bit = require('extra-bit');
18
21
  // import * as bit from "extra-bit";
19
22
  // import * as bit from "https://unpkg.com/extra-bit@1.0.50/index.mjs"; (deno)
20
23
 
@@ -40,20 +43,24 @@ bit.signExtend(15, 4);
40
43
 
41
44
  ## Index
42
45
 
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. |
46
+ | Property | Description |
47
+ | ---- | ---- |
48
+ | [get] | Get a bit. |
49
+ | [getAs] | Get bits as per mask. |
50
+ | [set] | Set a bit. |
51
+ | [setAs] | Set bits as per mask. |
52
+ | [toggle] | Toggle a bit. |
53
+ | [toggleAs] | Toggle bits as per mask. |
54
+ | [swap] | Swap bit sequences. |
55
+ | [scan] | Find index of first set bit from LSB. |
56
+ | [scanReverse] | Find index of first set bit from MSB. |
57
+ | [count] | Count bits set. |
58
+ | [parity] | Find n-bit parity. |
59
+ | [rotate] | Rotate bits. |
60
+ | [reverse] | Reverse all bits. |
61
+ | [merge] | Merge bits as per mask. |
62
+ | [interleave] | Interleave bits of two int16s. |
63
+ | [signExtend] | Sign extend variable bit-width integer. |
57
64
 
58
65
  <br>
59
66
  <br>
@@ -62,15 +69,19 @@ bit.signExtend(15, 4);
62
69
 
63
70
  [bit]: https://en.wikipedia.org/wiki/Bit
64
71
  [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
72
+ [get]: https://nodef.github.io/extra-bit/modules.html#get
73
+ [getAs]: https://nodef.github.io/extra-bit/modules.html#getAs
74
+ [set]: https://nodef.github.io/extra-bit/modules.html#set
75
+ [setAs]: https://nodef.github.io/extra-bit/modules.html#setAs
76
+ [toggle]: https://nodef.github.io/extra-bit/modules.html#toggle
77
+ [toggleAs]: https://nodef.github.io/extra-bit/modules.html#toggleAs
78
+ [swap]: https://nodef.github.io/extra-bit/modules.html#swap
79
+ [scan]: https://nodef.github.io/extra-bit/modules.html#scan
80
+ [scanReverse]: https://nodef.github.io/extra-bit/modules.html#scanReverse
81
+ [count]: https://nodef.github.io/extra-bit/modules.html#count
82
+ [parity]: https://nodef.github.io/extra-bit/modules.html#parity
83
+ [rotate]: https://nodef.github.io/extra-bit/modules.html#rotate
84
+ [reverse]: https://nodef.github.io/extra-bit/modules.html#reverse
85
+ [merge]: https://nodef.github.io/extra-bit/modules.html#merge
86
+ [interleave]: https://nodef.github.io/extra-bit/modules.html#interleave
87
+ [signExtend]: https://nodef.github.io/extra-bit/modules.html#signExtend
package/index.d.ts CHANGED
@@ -1,30 +1,24 @@
1
1
  /**
2
- * Get a bit.
2
+ * Get bits at specified index.
3
3
  * @param x an int32
4
4
  * @param i bit index
5
5
  * @param w bit width (1)
6
6
  */
7
7
  declare function get(x: number, i: number, w?: number): number;
8
- //# sourceMappingURL=get.d.ts.map
9
-
10
8
  /**
11
9
  * Get bits as per mask.
12
10
  * @param x an int32
13
11
  * @param m bit mask
14
12
  */
15
13
  declare function getAs(x: number, m: number): number;
16
- //# sourceMappingURL=getAs.d.ts.map
17
-
18
14
  /**
19
- * Set a bit.
15
+ * Set bits at specified index.
20
16
  * @param x an int32
21
17
  * @param i bit index
22
18
  * @param v bit value (1)
23
19
  * @param w bit width (1)
24
20
  */
25
21
  declare function set(x: number, i: number, v?: number, w?: number): number;
26
- //# sourceMappingURL=set.d.ts.map
27
-
28
22
  /**
29
23
  * Set bits as per mask.
30
24
  * @param x an int32
@@ -32,79 +26,59 @@ declare function set(x: number, i: number, v?: number, w?: number): number;
32
26
  * @param f bit value (1)
33
27
  */
34
28
  declare function setAs(x: number, m: number, f?: number): number;
35
- //# sourceMappingURL=setAs.d.ts.map
36
-
37
29
  /**
38
- * Toggle a bit.
30
+ * Toggle bits at specified index.
39
31
  * @param x an int32
40
32
  * @param i bit index
41
33
  * @param w bit width (1)
42
34
  */
43
35
  declare function toggle(x: number, i: number, w?: number): number;
44
- //# sourceMappingURL=toggle.d.ts.map
45
-
46
36
  /**
47
37
  * Toggle bits as per mask.
48
38
  * @param x an int32
49
39
  * @param m bit mask
50
40
  */
51
41
  declare function toggleAs(x: number, m: number): number;
52
- //# sourceMappingURL=toggleAs.d.ts.map
53
-
54
42
  /**
55
- * Swap bit sequences.
43
+ * Swap bit sequences at specified indices.
56
44
  * @param x an int32
57
45
  * @param i first bit index
58
46
  * @param j second bit index
59
47
  * @param w bit width (1)
60
48
  */
61
49
  declare function swap(x: number, i: number, j: number, w?: number): number;
62
- //# sourceMappingURL=swap.d.ts.map
63
-
64
50
  /**
65
51
  * Find index of first set bit from LSB.
66
52
  * @param x an int32
67
53
  */
68
54
  declare function scan(x: number): number;
69
- //# sourceMappingURL=scan.d.ts.map
70
-
71
55
  /**
72
56
  * Find index of first set bit from MSB.
73
57
  * @param x an int32
74
58
  */
75
59
  declare function scanReverse(x: number): number;
76
- //# sourceMappingURL=scanReverse.d.ts.map
77
-
78
60
  /**
79
61
  * Count bits set.
80
62
  * @param x an int32
81
63
  */
82
64
  declare function count(x: number): number;
83
- //# sourceMappingURL=count.d.ts.map
84
-
85
65
  /**
86
- * Find n-bit parity.
66
+ * Calculate n-bit parity.
87
67
  * @param x an int32
88
68
  * @param n number of bits (1)
89
69
  */
90
70
  declare function parity(x: number, n?: number): number;
91
- //# sourceMappingURL=parity.d.ts.map
92
-
93
71
  /**
94
- * Rotate bits.
72
+ * Rotate bits by a certain amount.
95
73
  * @param x an int32
96
74
  * @param n rotate amount (+ve: left, -ve: right)
97
75
  */
98
76
  declare function rotate(x: number, n?: number): number;
99
- //# sourceMappingURL=rotate.d.ts.map
100
-
101
77
  /**
102
78
  * Reverse all bits.
103
79
  * @param x an int32
104
80
  */
105
81
  declare function reverse(x: number): number;
106
- //# sourceMappingURL=reverse.d.ts.map
107
-
108
82
  /**
109
83
  * Merge bits as per mask.
110
84
  * @param x first int32
@@ -112,8 +86,6 @@ declare function reverse(x: number): number;
112
86
  * @param m bit mask (0 ⇒ from x)
113
87
  */
114
88
  declare function merge(x: number, y: number, m: number): number;
115
- //# sourceMappingURL=merge.d.ts.map
116
-
117
89
  /**
118
90
  * Interleave bits of two int16s.
119
91
  * @param x first int16
@@ -121,14 +93,11 @@ declare function merge(x: number, y: number, m: number): number;
121
93
  * @returns int32
122
94
  */
123
95
  declare function interleave(x: number, y: number): number;
124
- //# sourceMappingURL=interleave.d.ts.map
125
-
126
96
  /**
127
97
  * Sign extend variable bit-width integer.
128
98
  * @param x variable bit-width integer
129
99
  * @param w bit width (32)
130
100
  */
131
101
  declare function signExtend(x: number, w?: number): number;
132
- //# sourceMappingURL=signExtend.d.ts.map
133
102
 
134
103
  export { count, get, getAs, interleave, merge, parity, reverse, rotate, scan, scanReverse, set, setAs, signExtend, swap, toggle, toggleAs };
package/index.js CHANGED
@@ -5,67 +5,52 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  function ones(w) {
6
6
  return (1 << w) - 1;
7
7
  }
8
-
9
8
  function get(x, i, w = 1) {
10
9
  return (x >>> i) & ones(w);
11
10
  }
12
-
13
11
  function getAs(x, m) {
14
12
  return x & m;
15
13
  }
16
-
17
14
  function set(x, i, v = 1, w = 1) {
18
15
  return (x & ~(ones(w) << i)) | (v << i);
19
16
  }
20
-
21
17
  function setAs(x, m, f = 1) {
22
18
  return (x & ~m) | (-f & m);
23
19
  }
24
-
25
20
  function toggle(x, i, w = 1) {
26
21
  return x ^ (ones(w) << i);
27
22
  }
28
-
29
23
  function toggleAs(x, m) {
30
24
  return x ^ m;
31
25
  }
32
-
33
26
  function swap(x, i, j, w = 1) {
34
27
  var t = ((x >>> i) ^ (x >>> j)) & ones(w);
35
28
  return x ^ ((t << i) | (t << j));
36
29
  }
37
-
38
30
  const MOD37_POS32 = [
39
31
  32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
40
32
  25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
41
33
  ];
42
- var MOD37_POS32$1 = MOD37_POS32;
43
-
44
34
  function scan(x) {
45
- return MOD37_POS32$1[(-x & x) % 37];
35
+ return MOD37_POS32[(-x & x) % 37];
46
36
  }
47
-
48
37
  const DEBRUIJN_POS32 = [
49
38
  0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
50
39
  8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
51
40
  ];
52
- var DEBRUIJN_POS32$1 = DEBRUIJN_POS32;
53
-
54
41
  function scanReverse(x) {
55
42
  x |= x >>> 1;
56
43
  x |= x >>> 2;
57
44
  x |= x >>> 4;
58
45
  x |= x >>> 8;
59
46
  x |= x >>> 16;
60
- return DEBRUIJN_POS32$1[(x * 0x07C4ACDD) >>> 27];
47
+ return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
61
48
  }
62
-
63
49
  function count(x) {
64
50
  x = x - ((x >>> 1) & 0x55555555);
65
51
  x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
66
52
  return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
67
53
  }
68
-
69
54
  function parity1(x) {
70
55
  x ^= x >>> 16;
71
56
  x ^= x >>> 8;
@@ -83,11 +68,9 @@ function parity(x, n = 1) {
83
68
  }
84
69
  return a;
85
70
  }
86
-
87
71
  function rotate(x, n = 0) {
88
72
  return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
89
73
  }
90
-
91
74
  function reverse(x) {
92
75
  x = ((x >>> 1) & 0x55555555) | ((x & 0x55555555) << 1);
93
76
  x = ((x >>> 2) & 0x33333333) | ((x & 0x33333333) << 2);
@@ -95,11 +78,9 @@ function reverse(x) {
95
78
  x = ((x >>> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
96
79
  return (x >>> 16) | (x << 16);
97
80
  }
98
-
99
81
  function merge(x, y, m) {
100
82
  return x ^ ((x ^ y) & m);
101
83
  }
102
-
103
84
  function interleave(x, y) {
104
85
  x = (x | (x << 8)) & 0x00FF00FF;
105
86
  x = (x | (x << 4)) & 0x0F0F0F0F;
@@ -111,7 +92,6 @@ function interleave(x, y) {
111
92
  y = (y | (y << 1)) & 0x55555555;
112
93
  return y | (x << 1);
113
94
  }
114
-
115
95
  function signExtend(x, w = 32) {
116
96
  w = 32 - w;
117
97
  return (x << w) >> w;
package/index.mjs CHANGED
@@ -1,67 +1,52 @@
1
1
  function ones(w) {
2
2
  return (1 << w) - 1;
3
3
  }
4
-
5
4
  function get(x, i, w = 1) {
6
5
  return (x >>> i) & ones(w);
7
6
  }
8
-
9
7
  function getAs(x, m) {
10
8
  return x & m;
11
9
  }
12
-
13
10
  function set(x, i, v = 1, w = 1) {
14
11
  return (x & ~(ones(w) << i)) | (v << i);
15
12
  }
16
-
17
13
  function setAs(x, m, f = 1) {
18
14
  return (x & ~m) | (-f & m);
19
15
  }
20
-
21
16
  function toggle(x, i, w = 1) {
22
17
  return x ^ (ones(w) << i);
23
18
  }
24
-
25
19
  function toggleAs(x, m) {
26
20
  return x ^ m;
27
21
  }
28
-
29
22
  function swap(x, i, j, w = 1) {
30
23
  var t = ((x >>> i) ^ (x >>> j)) & ones(w);
31
24
  return x ^ ((t << i) | (t << j));
32
25
  }
33
-
34
26
  const MOD37_POS32 = [
35
27
  32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
36
28
  25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
37
29
  ];
38
- var MOD37_POS32$1 = MOD37_POS32;
39
-
40
30
  function scan(x) {
41
- return MOD37_POS32$1[(-x & x) % 37];
31
+ return MOD37_POS32[(-x & x) % 37];
42
32
  }
43
-
44
33
  const DEBRUIJN_POS32 = [
45
34
  0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
46
35
  8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
47
36
  ];
48
- var DEBRUIJN_POS32$1 = DEBRUIJN_POS32;
49
-
50
37
  function scanReverse(x) {
51
38
  x |= x >>> 1;
52
39
  x |= x >>> 2;
53
40
  x |= x >>> 4;
54
41
  x |= x >>> 8;
55
42
  x |= x >>> 16;
56
- return DEBRUIJN_POS32$1[(x * 0x07C4ACDD) >>> 27];
43
+ return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
57
44
  }
58
-
59
45
  function count(x) {
60
46
  x = x - ((x >>> 1) & 0x55555555);
61
47
  x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
62
48
  return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
63
49
  }
64
-
65
50
  function parity1(x) {
66
51
  x ^= x >>> 16;
67
52
  x ^= x >>> 8;
@@ -79,11 +64,9 @@ function parity(x, n = 1) {
79
64
  }
80
65
  return a;
81
66
  }
82
-
83
67
  function rotate(x, n = 0) {
84
68
  return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
85
69
  }
86
-
87
70
  function reverse(x) {
88
71
  x = ((x >>> 1) & 0x55555555) | ((x & 0x55555555) << 1);
89
72
  x = ((x >>> 2) & 0x33333333) | ((x & 0x33333333) << 2);
@@ -91,11 +74,9 @@ function reverse(x) {
91
74
  x = ((x >>> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
92
75
  return (x >>> 16) | (x << 16);
93
76
  }
94
-
95
77
  function merge(x, y, m) {
96
78
  return x ^ ((x ^ y) & m);
97
79
  }
98
-
99
80
  function interleave(x, y) {
100
81
  x = (x | (x << 8)) & 0x00FF00FF;
101
82
  x = (x | (x << 4)) & 0x0F0F0F0F;
@@ -107,7 +88,6 @@ function interleave(x, y) {
107
88
  y = (y | (y << 1)) & 0x55555555;
108
89
  return y | (x << 1);
109
90
  }
110
-
111
91
  function signExtend(x, w = 32) {
112
92
  w = 32 - w;
113
93
  return (x << w) >> w;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-bit",
3
- "version": "2.0.2",
3
+ "version": "2.1.1",
4
4
  "description": "The bit is a basic unit of information in information theory, computing.",
5
5
  "sideEffects": false,
6
6
  "main": "index.js",
@@ -45,11 +45,10 @@
45
45
  },
46
46
  "homepage": "https://github.com/nodef/extra-bit#readme",
47
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",
48
+ "@rollup/plugin-commonjs": "^22.0.1",
49
+ "@rollup/plugin-node-resolve": "^13.3.0",
50
+ "extra-build": "^2.1.31",
52
51
  "rollup-plugin-cleanup": "^3.2.1",
53
- "rollup-plugin-dts": "^4.2.1"
52
+ "rollup-plugin-dts": "^4.2.2"
54
53
  }
55
54
  }