BigIntBitwiseOp.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. // var $BigInt = GetIntrinsic('%BigInt%', true);
  5. // var $pow = GetIntrinsic('%Math.pow%');
  6. // var BinaryAnd = require('./BinaryAnd');
  7. // var BinaryOr = require('./BinaryOr');
  8. // var BinaryXor = require('./BinaryXor');
  9. var Type = require('./Type');
  10. // var modulo = require('./modulo');
  11. // var zero = $BigInt && $BigInt(0);
  12. // var negOne = $BigInt && $BigInt(-1);
  13. // var two = $BigInt && $BigInt(2);
  14. // https://262.ecma-international.org/11.0/#sec-bigintbitwiseop
  15. module.exports = function BigIntBitwiseOp(op, x, y) {
  16. if (op !== '&' && op !== '|' && op !== '^') {
  17. throw new $TypeError('Assertion failed: `op` must be `&`, `|`, or `^`');
  18. }
  19. if (Type(x) !== 'BigInt' || Type(y) !== 'BigInt') {
  20. throw new $TypeError('`x` and `y` must be BigInts');
  21. }
  22. if (op === '&') {
  23. return x & y;
  24. }
  25. if (op === '|') {
  26. return x | y;
  27. }
  28. return x ^ y;
  29. /*
  30. var result = zero;
  31. var shift = 0;
  32. while (x !== zero && x !== negOne && y !== zero && y !== negOne) {
  33. var xDigit = modulo(x, two);
  34. var yDigit = modulo(y, two);
  35. if (op === '&') {
  36. result += $pow(2, shift) * BinaryAnd(xDigit, yDigit);
  37. } else if (op === '|') {
  38. result += $pow(2, shift) * BinaryOr(xDigit, yDigit);
  39. } else if (op === '^') {
  40. result += $pow(2, shift) * BinaryXor(xDigit, yDigit);
  41. }
  42. shift += 1;
  43. x = (x - xDigit) / two;
  44. y = (y - yDigit) / two;
  45. }
  46. var tmp;
  47. if (op === '&') {
  48. tmp = BinaryAnd(modulo(x, two), modulo(y, two));
  49. } else if (op === '|') {
  50. tmp = BinaryAnd(modulo(x, two), modulo(y, two));
  51. } else {
  52. tmp = BinaryXor(modulo(x, two), modulo(y, two));
  53. }
  54. if (tmp !== 0) {
  55. result -= $pow(2, shift);
  56. }
  57. return result;
  58. */
  59. };