@obinexusmk2/hypernum 0.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 +21 -0
- package/README.md +256 -0
- package/dist/index.cjs +3425 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +1449 -0
- package/dist/index.js +3284 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +8 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/types/config/config-loader.d.ts +56 -0
- package/dist/types/config/config-loader.d.ts.map +1 -0
- package/dist/types/config/config-parser.d.ts +28 -0
- package/dist/types/config/config-parser.d.ts.map +1 -0
- package/dist/types/config/config-resolver.d.ts +21 -0
- package/dist/types/config/config-resolver.d.ts.map +1 -0
- package/dist/types/config/config-source.d.ts +27 -0
- package/dist/types/config/config-source.d.ts.map +1 -0
- package/dist/types/config/index.d.ts +68 -0
- package/dist/types/config/index.d.ts.map +1 -0
- package/dist/types/core/common.d.ts +169 -0
- package/dist/types/core/common.d.ts.map +1 -0
- package/dist/types/core/config.d.ts +197 -0
- package/dist/types/core/config.d.ts.map +1 -0
- package/dist/types/core/constants.d.ts +88 -0
- package/dist/types/core/constants.d.ts.map +1 -0
- package/dist/types/core/errors.d.ts +97 -0
- package/dist/types/core/errors.d.ts.map +1 -0
- package/dist/types/core/hypernum.d.ts +60 -0
- package/dist/types/core/hypernum.d.ts.map +1 -0
- package/dist/types/core/index.d.ts +6 -0
- package/dist/types/core/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +33 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/operations/arithmetic.d.ts +72 -0
- package/dist/types/operations/arithmetic.d.ts.map +1 -0
- package/dist/types/operations/bitwise.d.ts +98 -0
- package/dist/types/operations/bitwise.d.ts.map +1 -0
- package/dist/types/operations/comparison.d.ts +94 -0
- package/dist/types/operations/comparison.d.ts.map +1 -0
- package/dist/types/operations/conversion.d.ts +79 -0
- package/dist/types/operations/conversion.d.ts.map +1 -0
- package/dist/types/operations/factorial.d.ts +58 -0
- package/dist/types/operations/factorial.d.ts.map +1 -0
- package/dist/types/operations/index.d.ts +6 -0
- package/dist/types/operations/index.d.ts.map +1 -0
- package/dist/types/operations/power.d.ts +49 -0
- package/dist/types/operations/power.d.ts.map +1 -0
- package/dist/types/storage/Heap.d.ts +95 -0
- package/dist/types/storage/Heap.d.ts.map +1 -0
- package/dist/types/storage/index.d.ts +2 -0
- package/dist/types/storage/index.d.ts.map +1 -0
- package/dist/types/structures/ackermann.d.ts +74 -0
- package/dist/types/structures/ackermann.d.ts.map +1 -0
- package/dist/types/structures/big-array.d.ts +102 -0
- package/dist/types/structures/big-array.d.ts.map +1 -0
- package/dist/types/structures/index.d.ts +5 -0
- package/dist/types/structures/index.d.ts.map +1 -0
- package/dist/types/structures/number-tree.d.ts +114 -0
- package/dist/types/structures/number-tree.d.ts.map +1 -0
- package/dist/types/structures/power-tower.d.ts +74 -0
- package/dist/types/structures/power-tower.d.ts.map +1 -0
- package/dist/types/utils/formatting.d.ts +45 -0
- package/dist/types/utils/formatting.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +5 -0
- package/dist/types/utils/index.d.ts.map +1 -0
- package/dist/types/utils/parser.d.ts +39 -0
- package/dist/types/utils/parser.d.ts.map +1 -0
- package/dist/types/utils/precision.d.ts +57 -0
- package/dist/types/utils/precision.d.ts.map +1 -0
- package/dist/types/utils/validation.d.ts +28 -0
- package/dist/types/utils/validation.d.ts.map +1 -0
- package/package.json +164 -0
- package/rollup.config.js +162 -0
- package/src/config/config-loader.ts +226 -0
- package/src/config/config-parser.ts +161 -0
- package/src/config/config-resolver.ts +52 -0
- package/src/config/config-source.ts +32 -0
- package/src/config/index.ts +159 -0
- package/src/core/common.ts +185 -0
- package/src/core/config.ts +393 -0
- package/src/core/constants.ts +102 -0
- package/src/core/errors.ts +203 -0
- package/src/core/hypernum.ts +241 -0
- package/src/core/index.ts +5 -0
- package/src/index.ts +183 -0
- package/src/operations/arithmetic.ts +333 -0
- package/src/operations/bitwise.ts +367 -0
- package/src/operations/comparison.ts +272 -0
- package/src/operations/conversion.ts +400 -0
- package/src/operations/factorial.ts +279 -0
- package/src/operations/index.ts +5 -0
- package/src/operations/power.ts +316 -0
- package/src/storage/Heap.ts +238 -0
- package/src/storage/index.ts +1 -0
- package/src/structures/ackermann.ts +233 -0
- package/src/structures/big-array.ts +306 -0
- package/src/structures/index.ts +4 -0
- package/src/structures/number-tree.ts +404 -0
- package/src/structures/power-tower.ts +278 -0
- package/src/types/common.d.ts +357 -0
- package/src/types/core.d.ts +161 -0
- package/src/types/index.d.ts +2 -0
- package/src/utils/formatting.ts +246 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/parser.ts +245 -0
- package/src/utils/precision.ts +217 -0
- package/src/utils/validation.ts +183 -0
- package/tsconfig.json +84 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arithmetic operations module for Hypernum library
|
|
3
|
+
* Provides high-precision arithmetic operations with BigInt support
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
validateNonNegative,
|
|
8
|
+
toBigInt,
|
|
9
|
+
checkAdditionOverflow,
|
|
10
|
+
checkMultiplicationOverflow,
|
|
11
|
+
checkPowerOverflow,
|
|
12
|
+
ValidationError,
|
|
13
|
+
} from '../utils/validation';
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
RoundingMode,
|
|
17
|
+
round,
|
|
18
|
+
scaledDivision,
|
|
19
|
+
normalizePrecision,
|
|
20
|
+
} from '../utils/precision';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Options for arithmetic operations
|
|
24
|
+
*/
|
|
25
|
+
export interface ArithmeticOptions {
|
|
26
|
+
precision?: number;
|
|
27
|
+
roundingMode?: RoundingMode;
|
|
28
|
+
checkOverflow?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DEFAULT_OPTIONS: Required<ArithmeticOptions> = {
|
|
32
|
+
precision: 0,
|
|
33
|
+
roundingMode: RoundingMode.HALF_EVEN,
|
|
34
|
+
checkOverflow: true
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Adds two numbers with optional precision and overflow checking
|
|
39
|
+
*/
|
|
40
|
+
export function add(
|
|
41
|
+
a: bigint | string | number,
|
|
42
|
+
b: bigint | string | number,
|
|
43
|
+
options: ArithmeticOptions = {}
|
|
44
|
+
): bigint {
|
|
45
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
46
|
+
const bigA = toBigInt(a);
|
|
47
|
+
const bigB = toBigInt(b);
|
|
48
|
+
|
|
49
|
+
if (opts.checkOverflow) {
|
|
50
|
+
checkAdditionOverflow(bigA, bigB);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (opts.precision === 0) {
|
|
54
|
+
return bigA + bigB;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const [scaledA, scaledB] = normalizePrecision(bigA, bigB, opts.precision, opts.precision);
|
|
58
|
+
const result = scaledA + scaledB;
|
|
59
|
+
|
|
60
|
+
return round(result, opts.precision, opts.roundingMode);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Subtracts two numbers with optional precision and overflow checking
|
|
65
|
+
*/
|
|
66
|
+
export function subtract(
|
|
67
|
+
a: bigint | string | number,
|
|
68
|
+
b: bigint | string | number,
|
|
69
|
+
options: ArithmeticOptions = {}
|
|
70
|
+
): bigint {
|
|
71
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
72
|
+
const bigA = toBigInt(a);
|
|
73
|
+
const bigB = toBigInt(b);
|
|
74
|
+
|
|
75
|
+
if (opts.checkOverflow) {
|
|
76
|
+
checkAdditionOverflow(bigA, -bigB);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (opts.precision === 0) {
|
|
80
|
+
return bigA - bigB;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const [scaledA, scaledB] = normalizePrecision(bigA, bigB, opts.precision, opts.precision);
|
|
84
|
+
const result = scaledA - scaledB;
|
|
85
|
+
|
|
86
|
+
return round(result, opts.precision, opts.roundingMode);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Multiplies two numbers with optional precision and overflow checking
|
|
91
|
+
*/
|
|
92
|
+
export function multiply(
|
|
93
|
+
a: bigint | string | number,
|
|
94
|
+
b: bigint | string | number,
|
|
95
|
+
options: ArithmeticOptions = {}
|
|
96
|
+
): bigint {
|
|
97
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
98
|
+
const bigA = toBigInt(a);
|
|
99
|
+
const bigB = toBigInt(b);
|
|
100
|
+
|
|
101
|
+
if (opts.checkOverflow) {
|
|
102
|
+
checkMultiplicationOverflow(bigA, bigB);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const result = bigA * bigB;
|
|
106
|
+
if (opts.precision === 0) {
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return round(result, opts.precision, opts.roundingMode);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Divides two numbers with specified precision and rounding
|
|
115
|
+
*/
|
|
116
|
+
export function divide(
|
|
117
|
+
numerator: bigint | string | number,
|
|
118
|
+
denominator: bigint | string | number,
|
|
119
|
+
options: ArithmeticOptions = {}
|
|
120
|
+
): bigint {
|
|
121
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
122
|
+
const bigNumerator = toBigInt(numerator);
|
|
123
|
+
const bigDenominator = toBigInt(denominator);
|
|
124
|
+
|
|
125
|
+
if (bigDenominator === BigInt(0)) {
|
|
126
|
+
throw new ValidationError('Division by zero');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return scaledDivision(
|
|
130
|
+
bigNumerator,
|
|
131
|
+
bigDenominator,
|
|
132
|
+
opts.precision,
|
|
133
|
+
opts.roundingMode
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Calculates remainder with optional precision
|
|
139
|
+
*/
|
|
140
|
+
export function remainder(
|
|
141
|
+
a: bigint | string | number,
|
|
142
|
+
b: bigint | string | number,
|
|
143
|
+
options: ArithmeticOptions = {}
|
|
144
|
+
): bigint {
|
|
145
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
146
|
+
const bigA = toBigInt(a);
|
|
147
|
+
const bigB = toBigInt(b);
|
|
148
|
+
|
|
149
|
+
if (bigB === BigInt(0)) {
|
|
150
|
+
throw new ValidationError('Division by zero in remainder operation');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (opts.precision === 0) {
|
|
154
|
+
return bigA % bigB;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const [scaledA, scaledB] = normalizePrecision(bigA, bigB, opts.precision, opts.precision);
|
|
158
|
+
const result = scaledA % scaledB;
|
|
159
|
+
|
|
160
|
+
return round(result, opts.precision, opts.roundingMode);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Raises a number to a power with optional precision
|
|
165
|
+
*/
|
|
166
|
+
export function power(
|
|
167
|
+
base: bigint | string | number,
|
|
168
|
+
exponent: bigint | string | number,
|
|
169
|
+
options: ArithmeticOptions = {}
|
|
170
|
+
): bigint {
|
|
171
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
172
|
+
const bigBase = toBigInt(base);
|
|
173
|
+
const bigExponent = toBigInt(exponent);
|
|
174
|
+
|
|
175
|
+
if (opts.checkOverflow) {
|
|
176
|
+
checkPowerOverflow(bigBase, bigExponent);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
validateNonNegative(bigExponent);
|
|
180
|
+
|
|
181
|
+
if (bigExponent === BigInt(0)) {
|
|
182
|
+
return BigInt(1);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (bigExponent === BigInt(1)) {
|
|
186
|
+
return bigBase;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
let result = bigBase;
|
|
190
|
+
let remaining = bigExponent - BigInt(1);
|
|
191
|
+
|
|
192
|
+
while (remaining > BigInt(0)) {
|
|
193
|
+
if (opts.checkOverflow) {
|
|
194
|
+
checkMultiplicationOverflow(result, bigBase);
|
|
195
|
+
}
|
|
196
|
+
result *= bigBase;
|
|
197
|
+
remaining--;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (opts.precision > 0) {
|
|
201
|
+
return round(result, opts.precision, opts.roundingMode);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Calculates the square root with specified precision
|
|
209
|
+
*/
|
|
210
|
+
export function sqrt(
|
|
211
|
+
value: bigint | string | number,
|
|
212
|
+
options: ArithmeticOptions = {}
|
|
213
|
+
): bigint {
|
|
214
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
215
|
+
const bigValue = toBigInt(value);
|
|
216
|
+
validateNonNegative(bigValue);
|
|
217
|
+
|
|
218
|
+
if (bigValue === BigInt(0)) {
|
|
219
|
+
return BigInt(0);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Scale up for precision
|
|
223
|
+
const scaleFactor = BigInt(10) ** BigInt(opts.precision * 2);
|
|
224
|
+
const scaled = bigValue * scaleFactor;
|
|
225
|
+
|
|
226
|
+
// Newton's method for square root
|
|
227
|
+
let x = scaled;
|
|
228
|
+
let y = (x + scaled / x) >> BigInt(1);
|
|
229
|
+
|
|
230
|
+
while (y < x) {
|
|
231
|
+
x = y;
|
|
232
|
+
y = (x + scaled / x) >> BigInt(1);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return round(x, opts.precision, opts.roundingMode);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Calculates the absolute value
|
|
240
|
+
*/
|
|
241
|
+
export function abs(value: bigint | string | number): bigint {
|
|
242
|
+
const bigValue = toBigInt(value);
|
|
243
|
+
return bigValue < BigInt(0) ? -bigValue : bigValue;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Returns the sign of a number (-1, 0, or 1)
|
|
248
|
+
*/
|
|
249
|
+
export function sign(value: bigint | string | number): bigint {
|
|
250
|
+
const bigValue = toBigInt(value);
|
|
251
|
+
if (bigValue < BigInt(0)) return BigInt(-1);
|
|
252
|
+
if (bigValue > BigInt(0)) return BigInt(1);
|
|
253
|
+
return BigInt(0);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Calculates the greatest common divisor of two numbers
|
|
258
|
+
*/
|
|
259
|
+
export function gcd(
|
|
260
|
+
a: bigint | string | number,
|
|
261
|
+
b: bigint | string | number
|
|
262
|
+
): bigint {
|
|
263
|
+
let bigA = abs(toBigInt(a));
|
|
264
|
+
let bigB = abs(toBigInt(b));
|
|
265
|
+
|
|
266
|
+
while (bigB !== BigInt(0)) {
|
|
267
|
+
const temp = bigB;
|
|
268
|
+
bigB = bigA % bigB;
|
|
269
|
+
bigA = temp;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return bigA;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Calculates the least common multiple of two numbers
|
|
277
|
+
*/
|
|
278
|
+
export function lcm(
|
|
279
|
+
a: bigint | string | number,
|
|
280
|
+
b: bigint | string | number
|
|
281
|
+
): bigint {
|
|
282
|
+
const bigA = abs(toBigInt(a));
|
|
283
|
+
const bigB = abs(toBigInt(b));
|
|
284
|
+
|
|
285
|
+
if (bigA === BigInt(0) || bigB === BigInt(0)) {
|
|
286
|
+
return BigInt(0);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return abs(bigA * bigB) / gcd(bigA, bigB);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// /**
|
|
293
|
+
// * Calculates factorial of a number
|
|
294
|
+
// */
|
|
295
|
+
// export function factorial(value: bigint | string | number): bigint {
|
|
296
|
+
// const bigValue = toBigInt(value);
|
|
297
|
+
// validateNonNegative(bigValue);
|
|
298
|
+
|
|
299
|
+
// if (bigValue > BigInt(1000)) {
|
|
300
|
+
// throw new OverflowError('Factorial input too large');
|
|
301
|
+
// }
|
|
302
|
+
|
|
303
|
+
// if (bigValue <= BigInt(1)) {
|
|
304
|
+
// return BigInt(1);
|
|
305
|
+
// }
|
|
306
|
+
|
|
307
|
+
// let result = BigInt(1);
|
|
308
|
+
// let current = BigInt(2);
|
|
309
|
+
|
|
310
|
+
// while (current <= bigValue) {
|
|
311
|
+
// result *= current;
|
|
312
|
+
// current++;
|
|
313
|
+
// }
|
|
314
|
+
|
|
315
|
+
// return result;
|
|
316
|
+
// }
|
|
317
|
+
|
|
318
|
+
export default {
|
|
319
|
+
add,
|
|
320
|
+
subtract,
|
|
321
|
+
multiply,
|
|
322
|
+
divide,
|
|
323
|
+
remainder,
|
|
324
|
+
power,
|
|
325
|
+
sqrt,
|
|
326
|
+
abs,
|
|
327
|
+
sign,
|
|
328
|
+
gcd,
|
|
329
|
+
lcm,
|
|
330
|
+
// factorial
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bitwise operations module for Hypernum library
|
|
3
|
+
* Provides functions for bit-level manipulations of large numbers
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
toBigInt,
|
|
8
|
+
ValidationError,
|
|
9
|
+
validateNonNegative
|
|
10
|
+
} from '../utils/validation';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Options for bitwise operations
|
|
14
|
+
*/
|
|
15
|
+
export interface BitwiseOptions {
|
|
16
|
+
/** Maximum bits to consider in operations */
|
|
17
|
+
maxBits?: number;
|
|
18
|
+
/** Whether to throw on overflow */
|
|
19
|
+
strict?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const DEFAULT_OPTIONS: Required<BitwiseOptions> = {
|
|
23
|
+
maxBits: 1024,
|
|
24
|
+
strict: true
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Validates shift amount is within reasonable bounds
|
|
29
|
+
*/
|
|
30
|
+
function validateShift(shift: bigint, options: Required<BitwiseOptions>): void {
|
|
31
|
+
if (shift < 0n) {
|
|
32
|
+
throw new ValidationError('Shift amount cannot be negative');
|
|
33
|
+
}
|
|
34
|
+
if (options.strict && shift >= BigInt(options.maxBits)) {
|
|
35
|
+
throw new ValidationError(`Shift amount exceeds maximum of ${options.maxBits} bits`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Performs bitwise AND operation
|
|
41
|
+
*/
|
|
42
|
+
export function and(
|
|
43
|
+
a: bigint | string | number,
|
|
44
|
+
b: bigint | string | number
|
|
45
|
+
): bigint {
|
|
46
|
+
const bigA = toBigInt(a);
|
|
47
|
+
const bigB = toBigInt(b);
|
|
48
|
+
|
|
49
|
+
return bigA & bigB;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Performs bitwise OR operation
|
|
54
|
+
*/
|
|
55
|
+
export function or(
|
|
56
|
+
a: bigint | string | number,
|
|
57
|
+
b: bigint | string | number
|
|
58
|
+
): bigint {
|
|
59
|
+
const bigA = toBigInt(a);
|
|
60
|
+
const bigB = toBigInt(b);
|
|
61
|
+
|
|
62
|
+
return bigA | bigB;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Performs bitwise XOR operation
|
|
67
|
+
*/
|
|
68
|
+
export function xor(
|
|
69
|
+
a: bigint | string | number,
|
|
70
|
+
b: bigint | string | number
|
|
71
|
+
): bigint {
|
|
72
|
+
const bigA = toBigInt(a);
|
|
73
|
+
const bigB = toBigInt(b);
|
|
74
|
+
|
|
75
|
+
return bigA ^ bigB;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Performs bitwise NOT operation
|
|
80
|
+
*/
|
|
81
|
+
export function not(
|
|
82
|
+
value: bigint | string | number
|
|
83
|
+
): bigint {
|
|
84
|
+
const bigValue = toBigInt(value);
|
|
85
|
+
|
|
86
|
+
return ~bigValue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Performs left shift operation
|
|
91
|
+
*/
|
|
92
|
+
export function leftShift(
|
|
93
|
+
value: bigint | string | number,
|
|
94
|
+
shift: bigint | string | number,
|
|
95
|
+
options: BitwiseOptions = {}
|
|
96
|
+
): bigint {
|
|
97
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
98
|
+
const bigValue = toBigInt(value);
|
|
99
|
+
const bigShift = toBigInt(shift);
|
|
100
|
+
|
|
101
|
+
validateShift(bigShift, opts);
|
|
102
|
+
return bigValue << bigShift;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Performs right shift operation
|
|
107
|
+
*/
|
|
108
|
+
export function rightShift(
|
|
109
|
+
value: bigint | string | number,
|
|
110
|
+
shift: bigint | string | number,
|
|
111
|
+
options: BitwiseOptions = {}
|
|
112
|
+
): bigint {
|
|
113
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
114
|
+
const bigValue = toBigInt(value);
|
|
115
|
+
const bigShift = toBigInt(shift);
|
|
116
|
+
|
|
117
|
+
validateShift(bigShift, opts);
|
|
118
|
+
return bigValue >> bigShift;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Performs unsigned right shift operation
|
|
123
|
+
* Note: BigInt doesn't have >>> operator, so we implement it manually
|
|
124
|
+
*/
|
|
125
|
+
export function unsignedRightShift(
|
|
126
|
+
value: bigint | string | number,
|
|
127
|
+
shift: bigint | string | number,
|
|
128
|
+
options: BitwiseOptions = {}
|
|
129
|
+
): bigint {
|
|
130
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
131
|
+
const bigValue = toBigInt(value);
|
|
132
|
+
const bigShift = toBigInt(shift);
|
|
133
|
+
|
|
134
|
+
validateShift(bigShift, opts);
|
|
135
|
+
|
|
136
|
+
if (bigValue >= 0n) {
|
|
137
|
+
return bigValue >> bigShift;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Handle negative numbers by first converting to positive
|
|
141
|
+
const mask = (1n << BigInt(opts.maxBits)) - 1n;
|
|
142
|
+
return (bigValue & mask) >> bigShift;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Rotates bits left by specified amount
|
|
147
|
+
*/
|
|
148
|
+
export function rotateLeft(
|
|
149
|
+
value: bigint | string | number,
|
|
150
|
+
rotation: bigint | string | number,
|
|
151
|
+
options: BitwiseOptions = {}
|
|
152
|
+
): bigint {
|
|
153
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
154
|
+
const bigValue = toBigInt(value);
|
|
155
|
+
let bigRotation = toBigInt(rotation);
|
|
156
|
+
|
|
157
|
+
validateNonNegative(bigRotation);
|
|
158
|
+
|
|
159
|
+
// Normalize rotation to be within maxBits
|
|
160
|
+
if (bigRotation >= BigInt(opts.maxBits)) {
|
|
161
|
+
bigRotation = bigRotation % BigInt(opts.maxBits);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (bigRotation === 0n) {
|
|
165
|
+
return bigValue;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const leftPart = leftShift(bigValue, bigRotation, opts);
|
|
169
|
+
const rightPart = unsignedRightShift(bigValue, BigInt(opts.maxBits) - bigRotation, opts);
|
|
170
|
+
|
|
171
|
+
return leftPart | rightPart;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Rotates bits right by specified amount
|
|
176
|
+
*/
|
|
177
|
+
export function rotateRight(
|
|
178
|
+
value: bigint | string | number,
|
|
179
|
+
rotation: bigint | string | number,
|
|
180
|
+
options: BitwiseOptions = {}
|
|
181
|
+
): bigint {
|
|
182
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
183
|
+
const bigValue = toBigInt(value);
|
|
184
|
+
let bigRotation = toBigInt(rotation);
|
|
185
|
+
|
|
186
|
+
validateNonNegative(bigRotation);
|
|
187
|
+
|
|
188
|
+
// Normalize rotation to be within maxBits
|
|
189
|
+
if (bigRotation >= BigInt(opts.maxBits)) {
|
|
190
|
+
bigRotation = bigRotation % BigInt(opts.maxBits);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (bigRotation === 0n) {
|
|
194
|
+
return bigValue;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const rightPart = unsignedRightShift(bigValue, bigRotation, opts);
|
|
198
|
+
const leftPart = leftShift(bigValue, BigInt(opts.maxBits) - bigRotation, opts);
|
|
199
|
+
|
|
200
|
+
return leftPart | rightPart;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Counts number of set bits (1s)
|
|
205
|
+
*/
|
|
206
|
+
export function popCount(
|
|
207
|
+
value: bigint | string | number,
|
|
208
|
+
options: BitwiseOptions = {}
|
|
209
|
+
): bigint {
|
|
210
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
211
|
+
let bigValue = toBigInt(value);
|
|
212
|
+
|
|
213
|
+
let count = 0n;
|
|
214
|
+
while (bigValue !== 0n) {
|
|
215
|
+
count += bigValue & 1n;
|
|
216
|
+
bigValue = unsignedRightShift(bigValue, 1n, opts);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return count;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Returns number of trailing zero bits
|
|
224
|
+
*/
|
|
225
|
+
export function trailingZeros(
|
|
226
|
+
value: bigint | string | number,
|
|
227
|
+
options: BitwiseOptions = {}
|
|
228
|
+
): bigint {
|
|
229
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
230
|
+
let bigValue = toBigInt(value);
|
|
231
|
+
|
|
232
|
+
if (bigValue === 0n) {
|
|
233
|
+
return BigInt(opts.maxBits);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
let count = 0n;
|
|
237
|
+
while ((bigValue & 1n) === 0n) {
|
|
238
|
+
count++;
|
|
239
|
+
bigValue = unsignedRightShift(bigValue, 1n, opts);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return count;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Returns number of leading zero bits
|
|
247
|
+
*/
|
|
248
|
+
export function leadingZeros(
|
|
249
|
+
value: bigint | string | number,
|
|
250
|
+
options: BitwiseOptions = {}
|
|
251
|
+
): bigint {
|
|
252
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
253
|
+
let bigValue = toBigInt(value);
|
|
254
|
+
|
|
255
|
+
if (bigValue === 0n) {
|
|
256
|
+
return BigInt(opts.maxBits);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
let count = 0n;
|
|
260
|
+
const msb = 1n << BigInt(opts.maxBits - 1);
|
|
261
|
+
|
|
262
|
+
while ((bigValue & msb) === 0n && count < BigInt(opts.maxBits)) {
|
|
263
|
+
count++;
|
|
264
|
+
bigValue = leftShift(bigValue, 1n, opts);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return count;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Returns bit at specified position
|
|
272
|
+
*/
|
|
273
|
+
export function getBit(
|
|
274
|
+
value: bigint | string | number,
|
|
275
|
+
position: bigint | string | number,
|
|
276
|
+
options: BitwiseOptions = {}
|
|
277
|
+
): boolean {
|
|
278
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
279
|
+
const bigValue = toBigInt(value);
|
|
280
|
+
const bigPosition = toBigInt(position);
|
|
281
|
+
|
|
282
|
+
validateNonNegative(bigPosition);
|
|
283
|
+
if (opts.strict && bigPosition >= BigInt(opts.maxBits)) {
|
|
284
|
+
throw new ValidationError(`Bit position exceeds maximum of ${opts.maxBits} bits`);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return (bigValue & (1n << bigPosition)) !== 0n;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Sets bit at specified position
|
|
292
|
+
*/
|
|
293
|
+
export function setBit(
|
|
294
|
+
value: bigint | string | number,
|
|
295
|
+
position: bigint | string | number,
|
|
296
|
+
options: BitwiseOptions = {}
|
|
297
|
+
): bigint {
|
|
298
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
299
|
+
const bigValue = toBigInt(value);
|
|
300
|
+
const bigPosition = toBigInt(position);
|
|
301
|
+
|
|
302
|
+
validateNonNegative(bigPosition);
|
|
303
|
+
if (opts.strict && bigPosition >= BigInt(opts.maxBits)) {
|
|
304
|
+
throw new ValidationError(`Bit position exceeds maximum of ${opts.maxBits} bits`);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return bigValue | (1n << bigPosition);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Clears bit at specified position
|
|
312
|
+
*/
|
|
313
|
+
export function clearBit(
|
|
314
|
+
value: bigint | string | number,
|
|
315
|
+
position: bigint | string | number,
|
|
316
|
+
options: BitwiseOptions = {}
|
|
317
|
+
): bigint {
|
|
318
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
319
|
+
const bigValue = toBigInt(value);
|
|
320
|
+
const bigPosition = toBigInt(position);
|
|
321
|
+
|
|
322
|
+
validateNonNegative(bigPosition);
|
|
323
|
+
if (opts.strict && bigPosition >= BigInt(opts.maxBits)) {
|
|
324
|
+
throw new ValidationError(`Bit position exceeds maximum of ${opts.maxBits} bits`);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return bigValue & ~(1n << bigPosition);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Toggles bit at specified position
|
|
332
|
+
*/
|
|
333
|
+
export function toggleBit(
|
|
334
|
+
value: bigint | string | number,
|
|
335
|
+
position: bigint | string | number,
|
|
336
|
+
options: BitwiseOptions = {}
|
|
337
|
+
): bigint {
|
|
338
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
339
|
+
const bigValue = toBigInt(value);
|
|
340
|
+
const bigPosition = toBigInt(position);
|
|
341
|
+
|
|
342
|
+
validateNonNegative(bigPosition);
|
|
343
|
+
if (opts.strict && bigPosition >= BigInt(opts.maxBits)) {
|
|
344
|
+
throw new ValidationError(`Bit position exceeds maximum of ${opts.maxBits} bits`);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return bigValue ^ (1n << bigPosition);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export default {
|
|
351
|
+
and,
|
|
352
|
+
or,
|
|
353
|
+
xor,
|
|
354
|
+
not,
|
|
355
|
+
leftShift,
|
|
356
|
+
rightShift,
|
|
357
|
+
unsignedRightShift,
|
|
358
|
+
rotateLeft,
|
|
359
|
+
rotateRight,
|
|
360
|
+
popCount,
|
|
361
|
+
trailingZeros,
|
|
362
|
+
leadingZeros,
|
|
363
|
+
getBit,
|
|
364
|
+
setBit,
|
|
365
|
+
clearBit,
|
|
366
|
+
toggleBit
|
|
367
|
+
};
|