extra-bit 2.0.1 → 2.1.0
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 +1 -1
- package/README.md +47 -36
- package/index.d.ts +59 -254
- package/index.js +56 -53
- package/index.mjs +54 -53
- package/package.json +25 -21
- package/example.js +0 -11
package/LICENSE
CHANGED
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
|
44
|
-
|
|
|
45
|
-
| [get]
|
|
46
|
-
| [
|
|
47
|
-
| [
|
|
48
|
-
| [
|
|
49
|
-
| [
|
|
50
|
-
| [
|
|
51
|
-
| [
|
|
52
|
-
| [
|
|
53
|
-
| [
|
|
54
|
-
| [
|
|
55
|
-
| [
|
|
56
|
-
| [
|
|
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.
|
|
66
|
-
[
|
|
67
|
-
[
|
|
68
|
-
[
|
|
69
|
-
[
|
|
70
|
-
[
|
|
71
|
-
[
|
|
72
|
-
[
|
|
73
|
-
[
|
|
74
|
-
[
|
|
75
|
-
[
|
|
76
|
-
[rotate]: https://github.
|
|
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,298 +1,103 @@
|
|
|
1
|
-
declare module "extra-bit" {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Counts bits set.
|
|
5
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/count)
|
|
6
|
-
* @param x an int32
|
|
7
|
-
* @example
|
|
8
|
-
* ```javascript
|
|
9
|
-
* count(7);
|
|
10
|
-
* // 3 (111 ⇒ 3)
|
|
11
|
-
*
|
|
12
|
-
* count(12);
|
|
13
|
-
* // 2 (1100 ⇒ 2)
|
|
14
|
-
*
|
|
15
|
-
* count(63);
|
|
16
|
-
* // 6 (111111 ⇒ 6)
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
export function count(x: number): number;
|
|
20
1
|
/**
|
|
21
|
-
*
|
|
22
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/get)
|
|
2
|
+
* Get bits at specified index.
|
|
23
3
|
* @param x an int32
|
|
24
4
|
* @param i bit index
|
|
25
5
|
* @param w bit width (1)
|
|
26
|
-
* @example
|
|
27
|
-
* ```javascript
|
|
28
|
-
* get(6, 0);
|
|
29
|
-
* // 0 (110,0 ⇒ 0)
|
|
30
|
-
*
|
|
31
|
-
* get(6, 1);
|
|
32
|
-
* // 1 (110,1 ⇒ 1)
|
|
33
|
-
*
|
|
34
|
-
* get(6, 2);
|
|
35
|
-
* // 1 (110,2 ⇒ 1)
|
|
36
|
-
* ```
|
|
37
6
|
*/
|
|
38
|
-
|
|
7
|
+
declare function get(x: number, i: number, w?: number): number;
|
|
39
8
|
/**
|
|
40
|
-
*
|
|
41
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/getAs)
|
|
9
|
+
* Get bits as per mask.
|
|
42
10
|
* @param x an int32
|
|
43
11
|
* @param m bit mask
|
|
44
|
-
* @example
|
|
45
|
-
* ```javascript
|
|
46
|
-
* getAs(6, 4);
|
|
47
|
-
* // 4 (110,100 ⇒ 100)
|
|
48
|
-
*
|
|
49
|
-
* getAs(6, 7);
|
|
50
|
-
* // 6 (110,111 ⇒ 110)
|
|
51
|
-
*
|
|
52
|
-
* getAs(6, 5);
|
|
53
|
-
* // 4 (110,101 ⇒ 100)
|
|
54
|
-
* ```
|
|
55
12
|
*/
|
|
56
|
-
|
|
13
|
+
declare function getAs(x: number, m: number): number;
|
|
57
14
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* @param
|
|
61
|
-
* @param
|
|
62
|
-
* @
|
|
63
|
-
* @example
|
|
64
|
-
* ```javascript
|
|
65
|
-
* interleave(0x0000, 0xFFFF);
|
|
66
|
-
* // 1431655765 (0x55555555)
|
|
67
|
-
*
|
|
68
|
-
* interleave(0x1234, 0x1234);
|
|
69
|
-
* // 51121968 (0x030C0F30)
|
|
70
|
-
*
|
|
71
|
-
* interleave(0x1234, 0x4321);
|
|
72
|
-
* // 302845473 (0x120D0E21)
|
|
73
|
-
* ```
|
|
15
|
+
* Set bits at specified index.
|
|
16
|
+
* @param x an int32
|
|
17
|
+
* @param i bit index
|
|
18
|
+
* @param v bit value (1)
|
|
19
|
+
* @param w bit width (1)
|
|
74
20
|
*/
|
|
75
|
-
|
|
21
|
+
declare function set(x: number, i: number, v?: number, w?: number): number;
|
|
76
22
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* @param
|
|
80
|
-
* @param
|
|
81
|
-
* @param m bit mask (0 ⇒ from x)
|
|
82
|
-
* @example
|
|
83
|
-
* ```javascript
|
|
84
|
-
* merge(0x12, 0x24, 0x0F);
|
|
85
|
-
* // 20 (0x14)
|
|
86
|
-
*
|
|
87
|
-
* merge(0x1234, 0xABCD, 0x0F0F);
|
|
88
|
-
* // 6973 (0x1B3D)
|
|
89
|
-
*
|
|
90
|
-
* merge(0xAAAA, 0xBBBB, 0x3333);
|
|
91
|
-
* // 48059 (0xBBBB)
|
|
92
|
-
* ```
|
|
23
|
+
* Set bits as per mask.
|
|
24
|
+
* @param x an int32
|
|
25
|
+
* @param m bit mask
|
|
26
|
+
* @param f bit value (1)
|
|
93
27
|
*/
|
|
94
|
-
|
|
28
|
+
declare function setAs(x: number, m: number, f?: number): number;
|
|
95
29
|
/**
|
|
96
|
-
*
|
|
97
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/parity)
|
|
30
|
+
* Toggle bits at specified index.
|
|
98
31
|
* @param x an int32
|
|
99
|
-
* @param
|
|
100
|
-
* @
|
|
101
|
-
* ```javascript
|
|
102
|
-
* parity(7);
|
|
103
|
-
* // 1 (1,1,1 ⇒ 1)
|
|
104
|
-
*
|
|
105
|
-
* parity(5);
|
|
106
|
-
* // 0 (1,0,1 ⇒ 0)
|
|
107
|
-
*
|
|
108
|
-
* parity(8, 2);
|
|
109
|
-
* // 2 (10,00 ⇒ 10)
|
|
110
|
-
*
|
|
111
|
-
* parity(63, 4);
|
|
112
|
-
* // 12 (11,1111 ⇒ 1100)
|
|
113
|
-
* ```
|
|
32
|
+
* @param i bit index
|
|
33
|
+
* @param w bit width (1)
|
|
114
34
|
*/
|
|
115
|
-
|
|
35
|
+
declare function toggle(x: number, i: number, w?: number): number;
|
|
116
36
|
/**
|
|
117
|
-
*
|
|
118
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/reverse)
|
|
37
|
+
* Toggle bits as per mask.
|
|
119
38
|
* @param x an int32
|
|
120
|
-
* @
|
|
121
|
-
* ```javascript
|
|
122
|
-
* reverse(0xFFFF0000);
|
|
123
|
-
* // 65535 (0x0000FFFF)
|
|
124
|
-
*
|
|
125
|
-
* reverse(0x00AABBCC);
|
|
126
|
-
* // 870143232 (0x33DD5500)
|
|
127
|
-
*
|
|
128
|
-
* reverse(0x1234);
|
|
129
|
-
* // 742916096 (0x2C480000)
|
|
130
|
-
* ```
|
|
39
|
+
* @param m bit mask
|
|
131
40
|
*/
|
|
132
|
-
|
|
41
|
+
declare function toggleAs(x: number, m: number): number;
|
|
133
42
|
/**
|
|
134
|
-
*
|
|
135
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/rotate)
|
|
43
|
+
* Swap bit sequences at specified indices.
|
|
136
44
|
* @param x an int32
|
|
137
|
-
* @param
|
|
138
|
-
* @
|
|
139
|
-
*
|
|
140
|
-
* rotate(0x11112222, 4);
|
|
141
|
-
* // 286401057 (0x11122221)
|
|
142
|
-
*
|
|
143
|
-
* rotate(0x11112222, -4);
|
|
144
|
-
* // 554766882 (0x21111222)
|
|
145
|
-
* ```
|
|
45
|
+
* @param i first bit index
|
|
46
|
+
* @param j second bit index
|
|
47
|
+
* @param w bit width (1)
|
|
146
48
|
*/
|
|
147
|
-
|
|
49
|
+
declare function swap(x: number, i: number, j: number, w?: number): number;
|
|
148
50
|
/**
|
|
149
|
-
*
|
|
150
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/scan)
|
|
51
|
+
* Find index of first set bit from LSB.
|
|
151
52
|
* @param x an int32
|
|
152
|
-
* @example
|
|
153
|
-
* ```javascript
|
|
154
|
-
* scan(7);
|
|
155
|
-
* // 0 (111 ⇒ 0)
|
|
156
|
-
*
|
|
157
|
-
* scan(12);
|
|
158
|
-
* // 2 (1100 ⇒ 2)
|
|
159
|
-
*
|
|
160
|
-
* scan(64);
|
|
161
|
-
* // 6 (1000000 ⇒ 6)
|
|
162
|
-
* ```
|
|
163
53
|
*/
|
|
164
|
-
|
|
54
|
+
declare function scan(x: number): number;
|
|
165
55
|
/**
|
|
166
|
-
*
|
|
167
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/scanReverse)
|
|
56
|
+
* Find index of first set bit from MSB.
|
|
168
57
|
* @param x an int32
|
|
169
|
-
* @example
|
|
170
|
-
* ```javascript
|
|
171
|
-
* scanReverse(13);
|
|
172
|
-
* // 13 (1101 ⇒ 3)
|
|
173
|
-
*
|
|
174
|
-
* scanReverse(5);
|
|
175
|
-
* // 2 (101 ⇒ 2)
|
|
176
|
-
*
|
|
177
|
-
* scanReverse(1);
|
|
178
|
-
* // 0 (1 ⇒ 0)
|
|
179
|
-
* ```
|
|
180
58
|
*/
|
|
181
|
-
|
|
59
|
+
declare function scanReverse(x: number): number;
|
|
182
60
|
/**
|
|
183
|
-
*
|
|
184
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/set)
|
|
61
|
+
* Count bits set.
|
|
185
62
|
* @param x an int32
|
|
186
|
-
* @param i bit index
|
|
187
|
-
* @param v bit value (1)
|
|
188
|
-
* @param w bit width (1)
|
|
189
|
-
* @example
|
|
190
|
-
* ```javascript
|
|
191
|
-
* set(6, 0, 1);
|
|
192
|
-
* // 7 (110,0,1 ⇒ 111)
|
|
193
|
-
*
|
|
194
|
-
* set(6, 2, 1);
|
|
195
|
-
* // 6 (110,2,1 ⇒ 110)
|
|
196
|
-
*
|
|
197
|
-
* set(6, 2, 0);
|
|
198
|
-
* // 2 (110,2,0 ⇒ 010)
|
|
199
|
-
* ```
|
|
200
63
|
*/
|
|
201
|
-
|
|
64
|
+
declare function count(x: number): number;
|
|
202
65
|
/**
|
|
203
|
-
*
|
|
204
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/setAs)
|
|
66
|
+
* Calculate n-bit parity.
|
|
205
67
|
* @param x an int32
|
|
206
|
-
* @param
|
|
207
|
-
* @param f bit value (1)
|
|
208
|
-
* @example
|
|
209
|
-
* ```javascript
|
|
210
|
-
* setAs(8, 2, 1);
|
|
211
|
-
* // 10 (0x8 set 0x2 ⇒ 0xA)
|
|
212
|
-
*
|
|
213
|
-
* setAs(15, 3, 0);
|
|
214
|
-
* // 12 (0xF clear 0x3 ⇒ 0xC)
|
|
215
|
-
*
|
|
216
|
-
* setAs(0x1234, 0x430, 1);
|
|
217
|
-
* // 5684 (0x1234 set 0x430 ⇒ 0x1634)
|
|
218
|
-
* ```
|
|
68
|
+
* @param n number of bits (1)
|
|
219
69
|
*/
|
|
220
|
-
|
|
70
|
+
declare function parity(x: number, n?: number): number;
|
|
221
71
|
/**
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* @param
|
|
225
|
-
* @param w bit width (32)
|
|
226
|
-
* @example
|
|
227
|
-
* ```javascript
|
|
228
|
-
* signExtend(15, 4);
|
|
229
|
-
* // -1 (1111 ⇒ -1)
|
|
230
|
-
*
|
|
231
|
-
* signExtend(3, 3);
|
|
232
|
-
* // 3 (011 ⇒ 3)
|
|
233
|
-
*
|
|
234
|
-
* signExtend(4, 3);
|
|
235
|
-
* // -4 (100 ⇒ -4)
|
|
236
|
-
* ```
|
|
72
|
+
* Rotate bits by a certain amount.
|
|
73
|
+
* @param x an int32
|
|
74
|
+
* @param n rotate amount (+ve: left, -ve: right)
|
|
237
75
|
*/
|
|
238
|
-
|
|
76
|
+
declare function rotate(x: number, n?: number): number;
|
|
239
77
|
/**
|
|
240
|
-
*
|
|
241
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/swap)
|
|
78
|
+
* Reverse all bits.
|
|
242
79
|
* @param x an int32
|
|
243
|
-
* @param i first bit index
|
|
244
|
-
* @param j second bit index
|
|
245
|
-
* @param w bit width (1)
|
|
246
|
-
* @example
|
|
247
|
-
* ```javascript
|
|
248
|
-
* swap(6, 1, 0);
|
|
249
|
-
* // 5 (110 ⇒ 101)
|
|
250
|
-
*
|
|
251
|
-
* swap(0x1234, 8, 4, 4);
|
|
252
|
-
* // 4900 (0x1234 ⇒ 0x1324)
|
|
253
|
-
*
|
|
254
|
-
* swap(0x4AAB, 8, 0, 8);
|
|
255
|
-
* // 43850 (0x4AAB ⇒ 0xAB4A)
|
|
256
|
-
* ```
|
|
257
80
|
*/
|
|
258
|
-
|
|
81
|
+
declare function reverse(x: number): number;
|
|
259
82
|
/**
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
* @param
|
|
263
|
-
* @param
|
|
264
|
-
* @param w bit width (1)
|
|
265
|
-
* @example
|
|
266
|
-
* ```javascript
|
|
267
|
-
* toggle(6, 0);
|
|
268
|
-
* // 7 (110,0 ⇒ 111)
|
|
269
|
-
*
|
|
270
|
-
* toggle(6, 1);
|
|
271
|
-
* // 4 (110,1 ⇒ 100)
|
|
272
|
-
*
|
|
273
|
-
* toggle(6, 2);
|
|
274
|
-
* // 2 (110,2 ⇒ 010)
|
|
275
|
-
* ```
|
|
83
|
+
* Merge bits as per mask.
|
|
84
|
+
* @param x first int32
|
|
85
|
+
* @param y second int32
|
|
86
|
+
* @param m bit mask (0 ⇒ from x)
|
|
276
87
|
*/
|
|
277
|
-
|
|
88
|
+
declare function merge(x: number, y: number, m: number): number;
|
|
278
89
|
/**
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
* @param
|
|
282
|
-
* @
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
* // 1 (110,111 ⇒ 001)
|
|
290
|
-
*
|
|
291
|
-
* toggleAs(6, 3);
|
|
292
|
-
* // 5 (110,011 ⇒ 101)
|
|
293
|
-
* ```
|
|
90
|
+
* Interleave bits of two int16s.
|
|
91
|
+
* @param x first int16
|
|
92
|
+
* @param y second int16
|
|
93
|
+
* @returns int32
|
|
94
|
+
*/
|
|
95
|
+
declare function interleave(x: number, y: number): number;
|
|
96
|
+
/**
|
|
97
|
+
* Sign extend variable bit-width integer.
|
|
98
|
+
* @param x variable bit-width integer
|
|
99
|
+
* @param w bit width (32)
|
|
294
100
|
*/
|
|
295
|
-
|
|
101
|
+
declare function signExtend(x: number, w?: number): number;
|
|
296
102
|
|
|
297
|
-
export {};
|
|
298
|
-
}
|
|
103
|
+
export { count, get, getAs, interleave, merge, parity, reverse, rotate, scan, scanReverse, set, setAs, signExtend, swap, toggle, toggleAs };
|
package/index.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
|
|
2
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
-
|
|
4
|
-
x = x - ((x >>> 1) & 0x55555555);
|
|
5
|
-
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
|
|
6
|
-
return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
|
7
|
-
}
|
|
4
|
+
|
|
8
5
|
function ones(w) {
|
|
9
6
|
return (1 << w) - 1;
|
|
10
7
|
}
|
|
@@ -14,19 +11,45 @@ function get(x, i, w = 1) {
|
|
|
14
11
|
function getAs(x, m) {
|
|
15
12
|
return x & m;
|
|
16
13
|
}
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
x = (x | (x << 4)) & 0x0F0F0F0F;
|
|
20
|
-
x = (x | (x << 2)) & 0x33333333;
|
|
21
|
-
x = (x | (x << 1)) & 0x55555555;
|
|
22
|
-
y = (y | (y << 8)) & 0x00FF00FF;
|
|
23
|
-
y = (y | (y << 4)) & 0x0F0F0F0F;
|
|
24
|
-
y = (y | (y << 2)) & 0x33333333;
|
|
25
|
-
y = (y | (y << 1)) & 0x55555555;
|
|
26
|
-
return y | (x << 1);
|
|
14
|
+
function set(x, i, v = 1, w = 1) {
|
|
15
|
+
return (x & ~(ones(w) << i)) | (v << i);
|
|
27
16
|
}
|
|
28
|
-
function
|
|
29
|
-
return
|
|
17
|
+
function setAs(x, m, f = 1) {
|
|
18
|
+
return (x & ~m) | (-f & m);
|
|
19
|
+
}
|
|
20
|
+
function toggle(x, i, w = 1) {
|
|
21
|
+
return x ^ (ones(w) << i);
|
|
22
|
+
}
|
|
23
|
+
function toggleAs(x, m) {
|
|
24
|
+
return x ^ m;
|
|
25
|
+
}
|
|
26
|
+
function swap(x, i, j, w = 1) {
|
|
27
|
+
var t = ((x >>> i) ^ (x >>> j)) & ones(w);
|
|
28
|
+
return x ^ ((t << i) | (t << j));
|
|
29
|
+
}
|
|
30
|
+
const MOD37_POS32 = [
|
|
31
|
+
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
|
|
32
|
+
25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
|
|
33
|
+
];
|
|
34
|
+
function scan(x) {
|
|
35
|
+
return MOD37_POS32[(-x & x) % 37];
|
|
36
|
+
}
|
|
37
|
+
const DEBRUIJN_POS32 = [
|
|
38
|
+
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
39
|
+
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
40
|
+
];
|
|
41
|
+
function scanReverse(x) {
|
|
42
|
+
x |= x >>> 1;
|
|
43
|
+
x |= x >>> 2;
|
|
44
|
+
x |= x >>> 4;
|
|
45
|
+
x |= x >>> 8;
|
|
46
|
+
x |= x >>> 16;
|
|
47
|
+
return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
|
|
48
|
+
}
|
|
49
|
+
function count(x) {
|
|
50
|
+
x = x - ((x >>> 1) & 0x55555555);
|
|
51
|
+
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
|
|
52
|
+
return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
|
30
53
|
}
|
|
31
54
|
function parity1(x) {
|
|
32
55
|
x ^= x >>> 16;
|
|
@@ -45,6 +68,9 @@ function parity(x, n = 1) {
|
|
|
45
68
|
}
|
|
46
69
|
return a;
|
|
47
70
|
}
|
|
71
|
+
function rotate(x, n = 0) {
|
|
72
|
+
return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
|
|
73
|
+
}
|
|
48
74
|
function reverse(x) {
|
|
49
75
|
x = ((x >>> 1) & 0x55555555) | ((x & 0x55555555) << 1);
|
|
50
76
|
x = ((x >>> 2) & 0x33333333) | ((x & 0x33333333) << 2);
|
|
@@ -52,48 +78,25 @@ function reverse(x) {
|
|
|
52
78
|
x = ((x >>> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
|
|
53
79
|
return (x >>> 16) | (x << 16);
|
|
54
80
|
}
|
|
55
|
-
function
|
|
56
|
-
return
|
|
57
|
-
}
|
|
58
|
-
const MOD37_POS32 = [
|
|
59
|
-
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
|
|
60
|
-
25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
|
|
61
|
-
];
|
|
62
|
-
function scan(x) {
|
|
63
|
-
return MOD37_POS32[(-x & x) % 37];
|
|
64
|
-
}
|
|
65
|
-
const DEBRUIJN_POS32 = [
|
|
66
|
-
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
67
|
-
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
68
|
-
];
|
|
69
|
-
function scanReverse(x) {
|
|
70
|
-
x |= x >>> 1;
|
|
71
|
-
x |= x >>> 2;
|
|
72
|
-
x |= x >>> 4;
|
|
73
|
-
x |= x >>> 8;
|
|
74
|
-
x |= x >>> 16;
|
|
75
|
-
return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
|
|
76
|
-
}
|
|
77
|
-
function set(x, i, v = 1, w = 1) {
|
|
78
|
-
return (x & ~(ones(w) << i)) | (v << i);
|
|
81
|
+
function merge(x, y, m) {
|
|
82
|
+
return x ^ ((x ^ y) & m);
|
|
79
83
|
}
|
|
80
|
-
function
|
|
81
|
-
|
|
84
|
+
function interleave(x, y) {
|
|
85
|
+
x = (x | (x << 8)) & 0x00FF00FF;
|
|
86
|
+
x = (x | (x << 4)) & 0x0F0F0F0F;
|
|
87
|
+
x = (x | (x << 2)) & 0x33333333;
|
|
88
|
+
x = (x | (x << 1)) & 0x55555555;
|
|
89
|
+
y = (y | (y << 8)) & 0x00FF00FF;
|
|
90
|
+
y = (y | (y << 4)) & 0x0F0F0F0F;
|
|
91
|
+
y = (y | (y << 2)) & 0x33333333;
|
|
92
|
+
y = (y | (y << 1)) & 0x55555555;
|
|
93
|
+
return y | (x << 1);
|
|
82
94
|
}
|
|
83
95
|
function signExtend(x, w = 32) {
|
|
84
96
|
w = 32 - w;
|
|
85
97
|
return (x << w) >> w;
|
|
86
98
|
}
|
|
87
|
-
|
|
88
|
-
var t = ((x >>> i) ^ (x >>> j)) & ones(w);
|
|
89
|
-
return x ^ ((t << i) | (t << j));
|
|
90
|
-
}
|
|
91
|
-
function toggle(x, i, w = 1) {
|
|
92
|
-
return x ^ (ones(w) << i);
|
|
93
|
-
}
|
|
94
|
-
function toggleAs(x, m) {
|
|
95
|
-
return x ^ m;
|
|
96
|
-
}
|
|
99
|
+
|
|
97
100
|
exports.count = count;
|
|
98
101
|
exports.get = get;
|
|
99
102
|
exports.getAs = getAs;
|
package/index.mjs
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
function count(x) {
|
|
2
|
-
x = x - ((x >>> 1) & 0x55555555);
|
|
3
|
-
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
|
|
4
|
-
return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
|
5
|
-
}
|
|
6
1
|
function ones(w) {
|
|
7
2
|
return (1 << w) - 1;
|
|
8
3
|
}
|
|
@@ -12,19 +7,45 @@ function get(x, i, w = 1) {
|
|
|
12
7
|
function getAs(x, m) {
|
|
13
8
|
return x & m;
|
|
14
9
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
x = (x | (x << 4)) & 0x0F0F0F0F;
|
|
18
|
-
x = (x | (x << 2)) & 0x33333333;
|
|
19
|
-
x = (x | (x << 1)) & 0x55555555;
|
|
20
|
-
y = (y | (y << 8)) & 0x00FF00FF;
|
|
21
|
-
y = (y | (y << 4)) & 0x0F0F0F0F;
|
|
22
|
-
y = (y | (y << 2)) & 0x33333333;
|
|
23
|
-
y = (y | (y << 1)) & 0x55555555;
|
|
24
|
-
return y | (x << 1);
|
|
10
|
+
function set(x, i, v = 1, w = 1) {
|
|
11
|
+
return (x & ~(ones(w) << i)) | (v << i);
|
|
25
12
|
}
|
|
26
|
-
function
|
|
27
|
-
return
|
|
13
|
+
function setAs(x, m, f = 1) {
|
|
14
|
+
return (x & ~m) | (-f & m);
|
|
15
|
+
}
|
|
16
|
+
function toggle(x, i, w = 1) {
|
|
17
|
+
return x ^ (ones(w) << i);
|
|
18
|
+
}
|
|
19
|
+
function toggleAs(x, m) {
|
|
20
|
+
return x ^ m;
|
|
21
|
+
}
|
|
22
|
+
function swap(x, i, j, w = 1) {
|
|
23
|
+
var t = ((x >>> i) ^ (x >>> j)) & ones(w);
|
|
24
|
+
return x ^ ((t << i) | (t << j));
|
|
25
|
+
}
|
|
26
|
+
const MOD37_POS32 = [
|
|
27
|
+
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
|
|
28
|
+
25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
|
|
29
|
+
];
|
|
30
|
+
function scan(x) {
|
|
31
|
+
return MOD37_POS32[(-x & x) % 37];
|
|
32
|
+
}
|
|
33
|
+
const DEBRUIJN_POS32 = [
|
|
34
|
+
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
35
|
+
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
36
|
+
];
|
|
37
|
+
function scanReverse(x) {
|
|
38
|
+
x |= x >>> 1;
|
|
39
|
+
x |= x >>> 2;
|
|
40
|
+
x |= x >>> 4;
|
|
41
|
+
x |= x >>> 8;
|
|
42
|
+
x |= x >>> 16;
|
|
43
|
+
return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
|
|
44
|
+
}
|
|
45
|
+
function count(x) {
|
|
46
|
+
x = x - ((x >>> 1) & 0x55555555);
|
|
47
|
+
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
|
|
48
|
+
return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
|
28
49
|
}
|
|
29
50
|
function parity1(x) {
|
|
30
51
|
x ^= x >>> 16;
|
|
@@ -43,6 +64,9 @@ function parity(x, n = 1) {
|
|
|
43
64
|
}
|
|
44
65
|
return a;
|
|
45
66
|
}
|
|
67
|
+
function rotate(x, n = 0) {
|
|
68
|
+
return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
|
|
69
|
+
}
|
|
46
70
|
function reverse(x) {
|
|
47
71
|
x = ((x >>> 1) & 0x55555555) | ((x & 0x55555555) << 1);
|
|
48
72
|
x = ((x >>> 2) & 0x33333333) | ((x & 0x33333333) << 2);
|
|
@@ -50,46 +74,23 @@ function reverse(x) {
|
|
|
50
74
|
x = ((x >>> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
|
|
51
75
|
return (x >>> 16) | (x << 16);
|
|
52
76
|
}
|
|
53
|
-
function
|
|
54
|
-
return
|
|
55
|
-
}
|
|
56
|
-
const MOD37_POS32 = [
|
|
57
|
-
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
|
|
58
|
-
25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
|
|
59
|
-
];
|
|
60
|
-
function scan(x) {
|
|
61
|
-
return MOD37_POS32[(-x & x) % 37];
|
|
62
|
-
}
|
|
63
|
-
const DEBRUIJN_POS32 = [
|
|
64
|
-
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
65
|
-
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
66
|
-
];
|
|
67
|
-
function scanReverse(x) {
|
|
68
|
-
x |= x >>> 1;
|
|
69
|
-
x |= x >>> 2;
|
|
70
|
-
x |= x >>> 4;
|
|
71
|
-
x |= x >>> 8;
|
|
72
|
-
x |= x >>> 16;
|
|
73
|
-
return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
|
|
74
|
-
}
|
|
75
|
-
function set(x, i, v = 1, w = 1) {
|
|
76
|
-
return (x & ~(ones(w) << i)) | (v << i);
|
|
77
|
+
function merge(x, y, m) {
|
|
78
|
+
return x ^ ((x ^ y) & m);
|
|
77
79
|
}
|
|
78
|
-
function
|
|
79
|
-
|
|
80
|
+
function interleave(x, y) {
|
|
81
|
+
x = (x | (x << 8)) & 0x00FF00FF;
|
|
82
|
+
x = (x | (x << 4)) & 0x0F0F0F0F;
|
|
83
|
+
x = (x | (x << 2)) & 0x33333333;
|
|
84
|
+
x = (x | (x << 1)) & 0x55555555;
|
|
85
|
+
y = (y | (y << 8)) & 0x00FF00FF;
|
|
86
|
+
y = (y | (y << 4)) & 0x0F0F0F0F;
|
|
87
|
+
y = (y | (y << 2)) & 0x33333333;
|
|
88
|
+
y = (y | (y << 1)) & 0x55555555;
|
|
89
|
+
return y | (x << 1);
|
|
80
90
|
}
|
|
81
91
|
function signExtend(x, w = 32) {
|
|
82
92
|
w = 32 - w;
|
|
83
93
|
return (x << w) >> w;
|
|
84
94
|
}
|
|
85
|
-
|
|
86
|
-
var t = ((x >>> i) ^ (x >>> j)) & ones(w);
|
|
87
|
-
return x ^ ((t << i) | (t << j));
|
|
88
|
-
}
|
|
89
|
-
function toggle(x, i, w = 1) {
|
|
90
|
-
return x ^ (ones(w) << i);
|
|
91
|
-
}
|
|
92
|
-
function toggleAs(x, m) {
|
|
93
|
-
return x ^ m;
|
|
94
|
-
}
|
|
95
|
+
|
|
95
96
|
export { count, get, getAs, interleave, merge, parity, reverse, rotate, scan, scanReverse, set, setAs, signExtend, swap, toggle, toggleAs };
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extra-bit",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "The bit is a basic unit of information in information theory, computing.",
|
|
5
|
+
"sideEffects": false,
|
|
5
6
|
"main": "index.js",
|
|
6
7
|
"module": "index.mjs",
|
|
7
8
|
"exports": {
|
|
@@ -10,42 +11,45 @@
|
|
|
10
11
|
},
|
|
11
12
|
"scripts": {
|
|
12
13
|
"test": "exit",
|
|
13
|
-
"build": "
|
|
14
|
-
"deploy": "
|
|
15
|
-
},
|
|
16
|
-
"runkitExampleFilename": "example.js",
|
|
17
|
-
"repository": {
|
|
18
|
-
"type": "git",
|
|
19
|
-
"url": "git+https://github.com/nodef/extra-bit.git"
|
|
14
|
+
"build": "node ./build",
|
|
15
|
+
"deploy": "node ./build deploy"
|
|
20
16
|
},
|
|
21
17
|
"keywords": [
|
|
22
18
|
"extra",
|
|
23
19
|
"bit",
|
|
24
|
-
"count",
|
|
25
20
|
"get",
|
|
26
21
|
"getAs",
|
|
27
|
-
"interleave",
|
|
28
|
-
"merge",
|
|
29
|
-
"moveWithin",
|
|
30
|
-
"parity",
|
|
31
|
-
"reverse",
|
|
32
|
-
"rotate",
|
|
33
|
-
"scan",
|
|
34
|
-
"scanReverse",
|
|
35
22
|
"set",
|
|
36
23
|
"setAs",
|
|
37
|
-
"signExtend",
|
|
38
|
-
"swap",
|
|
39
24
|
"toggle",
|
|
40
|
-
"toggleAs"
|
|
25
|
+
"toggleAs",
|
|
26
|
+
"swap",
|
|
27
|
+
"scan",
|
|
28
|
+
"scanReverse",
|
|
29
|
+
"count",
|
|
30
|
+
"parity",
|
|
31
|
+
"rotate",
|
|
32
|
+
"reverse",
|
|
33
|
+
"merge",
|
|
34
|
+
"interleave",
|
|
35
|
+
"signExtend"
|
|
41
36
|
],
|
|
42
37
|
"author": "wolfram77@gmail.com",
|
|
43
38
|
"license": "MIT",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/nodef/extra-bit.git"
|
|
42
|
+
},
|
|
44
43
|
"bugs": {
|
|
45
44
|
"url": "https://github.com/nodef/extra-bit/issues"
|
|
46
45
|
},
|
|
47
46
|
"homepage": "https://github.com/nodef/extra-bit#readme",
|
|
48
47
|
"devDependencies": {
|
|
49
|
-
"
|
|
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"
|
|
50
54
|
}
|
|
51
55
|
}
|