BigIntBitwiseOp.js 1.6 KB

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