NumberBitwiseOp.js 681 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var ToInt32 = require('./ToInt32');
  4. var ToUint32 = require('./ToUint32');
  5. // https://262.ecma-international.org/11.0/#sec-numberbitwiseop
  6. module.exports = function NumberBitwiseOp(op, x, y) {
  7. if (op !== '&' && op !== '|' && op !== '^') {
  8. throw new $TypeError('Assertion failed: `op` must be `&`, `|`, or `^`');
  9. }
  10. if (typeof x !== 'number' || typeof y !== 'number') {
  11. throw new $TypeError('Assertion failed: `x` and `y` arguments must be Numbers');
  12. }
  13. var lnum = ToInt32(x);
  14. var rnum = ToUint32(y);
  15. if (op === '&') {
  16. return lnum & rnum;
  17. }
  18. if (op === '|') {
  19. return lnum | rnum;
  20. }
  21. return lnum ^ rnum;
  22. };