bigint.js (6321B)
1 // Copyright (C) 2017 Josh Wolfe. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 4 /*--- 5 description: Bitwise AND for BigInt values 6 esid: sec-bitwise-op 7 info: | 8 BitwiseOp(op, x, y) 9 10 1. Let result be 0. 11 2. Let shift be 0. 12 3. Repeat, until (x = 0 or x = -1) and (y = 0 or y = -1), 13 a. Let xDigit be x modulo 2. 14 b. Let yDigit be y modulo 2. 15 c. Let result be result + 2**shift * op(xDigit, yDigit) 16 d. Let shift be shift + 1. 17 e. Let x be (x - xDigit) / 2. 18 f. Let y be (y - yDigit) / 2. 19 4. If op(x modulo 2, y modulo 2) ≠ 0, 20 a. Let result be result - 2**shift. NOTE: This extends the sign. 21 5. Return result. 22 23 features: [BigInt] 24 ---*/ 25 26 assert.sameValue(0b00n & 0b00n, 0b00n, "0b00n & 0b00n === 0b00n"); 27 assert.sameValue(0b00n & 0b01n, 0b00n, "0b00n & 0b01n === 0b00n"); 28 assert.sameValue(0b01n & 0b00n, 0b00n, "0b01n & 0b00n === 0b00n"); 29 assert.sameValue(0b00n & 0b10n, 0b00n, "0b00n & 0b10n === 0b00n"); 30 assert.sameValue(0b10n & 0b00n, 0b00n, "0b10n & 0b00n === 0b00n"); 31 assert.sameValue(0b00n & 0b11n, 0b00n, "0b00n & 0b11n === 0b00n"); 32 assert.sameValue(0b11n & 0b00n, 0b00n, "0b11n & 0b00n === 0b00n"); 33 assert.sameValue(0b01n & 0b01n, 0b01n, "0b01n & 0b01n === 0b01n"); 34 assert.sameValue(0b01n & 0b10n, 0b00n, "0b01n & 0b10n === 0b00n"); 35 assert.sameValue(0b10n & 0b01n, 0b00n, "0b10n & 0b01n === 0b00n"); 36 assert.sameValue(0b01n & 0b11n, 0b01n, "0b01n & 0b11n === 0b01n"); 37 assert.sameValue(0b11n & 0b01n, 0b01n, "0b11n & 0b01n === 0b01n"); 38 assert.sameValue(0b10n & 0b10n, 0b10n, "0b10n & 0b10n === 0b10n"); 39 assert.sameValue(0b10n & 0b11n, 0b10n, "0b10n & 0b11n === 0b10n"); 40 assert.sameValue(0b11n & 0b10n, 0b10n, "0b11n & 0b10n === 0b10n"); 41 assert.sameValue(0xffffffffn & 0n, 0n, "0xffffffffn & 0n === 0n"); 42 assert.sameValue(0n & 0xffffffffn, 0n, "0n & 0xffffffffn === 0n"); 43 assert.sameValue(0xffffffffn & 0xffffffffn, 0xffffffffn, "0xffffffffn & 0xffffffffn === 0xffffffffn"); 44 assert.sameValue(0xffffffffffffffffn & 0n, 0n, "0xffffffffffffffffn & 0n === 0n"); 45 assert.sameValue(0n & 0xffffffffffffffffn, 0n, "0n & 0xffffffffffffffffn === 0n"); 46 assert.sameValue(0xffffffffffffffffn & 0xffffffffn, 0xffffffffn, "0xffffffffffffffffn & 0xffffffffn === 0xffffffffn"); 47 assert.sameValue(0xffffffffn & 0xffffffffffffffffn, 0xffffffffn, "0xffffffffn & 0xffffffffffffffffn === 0xffffffffn"); 48 assert.sameValue( 49 0xffffffffffffffffn & 0xffffffffffffffffn, 0xffffffffffffffffn, 50 "0xffffffffffffffffn & 0xffffffffffffffffn === 0xffffffffffffffffn"); 51 assert.sameValue( 52 0xbf2ed51ff75d380fd3be813ec6185780n & 0x4aabef2324cedff5387f1f65n, 0x42092803008e813400181700n, 53 "0xbf2ed51ff75d380fd3be813ec6185780n & 0x4aabef2324cedff5387f1f65n === 0x42092803008e813400181700n"); 54 assert.sameValue( 55 0x4aabef2324cedff5387f1f65n & 0xbf2ed51ff75d380fd3be813ec6185780n, 0x42092803008e813400181700n, 56 "0x4aabef2324cedff5387f1f65n & 0xbf2ed51ff75d380fd3be813ec6185780n === 0x42092803008e813400181700n"); 57 assert.sameValue(0n & -1n, 0n, "0n & -1n === 0n"); 58 assert.sameValue(-1n & 0n, 0n, "-1n & 0n === 0n"); 59 assert.sameValue(0n & -2n, 0n, "0n & -2n === 0n"); 60 assert.sameValue(-2n & 0n, 0n, "-2n & 0n === 0n"); 61 assert.sameValue(1n & -2n, 0n, "1n & -2n === 0n"); 62 assert.sameValue(-2n & 1n, 0n, "-2n & 1n === 0n"); 63 assert.sameValue(2n & -2n, 2n, "2n & -2n === 2n"); 64 assert.sameValue(-2n & 2n, 2n, "-2n & 2n === 2n"); 65 assert.sameValue(2n & -3n, 0n, "2n & -3n === 0n"); 66 assert.sameValue(-3n & 2n, 0n, "-3n & 2n === 0n"); 67 assert.sameValue(-1n & -2n, -2n, "-1n & -2n === -2n"); 68 assert.sameValue(-2n & -1n, -2n, "-2n & -1n === -2n"); 69 assert.sameValue(-2n & -2n, -2n, "-2n & -2n === -2n"); 70 assert.sameValue(-2n & -3n, -4n, "-2n & -3n === -4n"); 71 assert.sameValue(-3n & -2n, -4n, "-3n & -2n === -4n"); 72 assert.sameValue(0xffffffffn & -1n, 0xffffffffn, "0xffffffffn & -1n === 0xffffffffn"); 73 assert.sameValue(-1n & 0xffffffffn, 0xffffffffn, "-1n & 0xffffffffn === 0xffffffffn"); 74 assert.sameValue(0xffffffffffffffffn & -1n, 0xffffffffffffffffn, "0xffffffffffffffffn & -1n === 0xffffffffffffffffn"); 75 assert.sameValue(-1n & 0xffffffffffffffffn, 0xffffffffffffffffn, "-1n & 0xffffffffffffffffn === 0xffffffffffffffffn"); 76 assert.sameValue( 77 0xbf2ed51ff75d380fd3be813ec6185780n & -0x4aabef2324cedff5387f1f65n, 0xbf2ed51fb554100cd330000ac6004080n, 78 "0xbf2ed51ff75d380fd3be813ec6185780n & -0x4aabef2324cedff5387f1f65n === 0xbf2ed51fb554100cd330000ac6004080n"); 79 assert.sameValue( 80 -0x4aabef2324cedff5387f1f65n & 0xbf2ed51ff75d380fd3be813ec6185780n, 0xbf2ed51fb554100cd330000ac6004080n, 81 "-0x4aabef2324cedff5387f1f65n & 0xbf2ed51ff75d380fd3be813ec6185780n === 0xbf2ed51fb554100cd330000ac6004080n"); 82 assert.sameValue( 83 -0xbf2ed51ff75d380fd3be813ec6185780n & 0x4aabef2324cedff5387f1f65n, 0x8a2c72024405ec138670800n, 84 "-0xbf2ed51ff75d380fd3be813ec6185780n & 0x4aabef2324cedff5387f1f65n === 0x8a2c72024405ec138670800n"); 85 assert.sameValue( 86 0x4aabef2324cedff5387f1f65n & -0xbf2ed51ff75d380fd3be813ec6185780n, 0x8a2c72024405ec138670800n, 87 "0x4aabef2324cedff5387f1f65n & -0xbf2ed51ff75d380fd3be813ec6185780n === 0x8a2c72024405ec138670800n"); 88 assert.sameValue( 89 -0xbf2ed51ff75d380fd3be813ec6185780n & -0x4aabef2324cedff5387f1f65n, -0xbf2ed51fffffff2ff7fedffffe7f5f80n, 90 "-0xbf2ed51ff75d380fd3be813ec6185780n & -0x4aabef2324cedff5387f1f65n === -0xbf2ed51fffffff2ff7fedffffe7f5f80n"); 91 assert.sameValue( 92 -0x4aabef2324cedff5387f1f65n & -0xbf2ed51ff75d380fd3be813ec6185780n, -0xbf2ed51fffffff2ff7fedffffe7f5f80n, 93 "-0x4aabef2324cedff5387f1f65n & -0xbf2ed51ff75d380fd3be813ec6185780n === -0xbf2ed51fffffff2ff7fedffffe7f5f80n"); 94 assert.sameValue(-0xffffffffn & 0n, 0n, "-0xffffffffn & 0n === 0n"); 95 assert.sameValue(0n & -0xffffffffn, 0n, "0n & -0xffffffffn === 0n"); 96 assert.sameValue( 97 -0xffffffffffffffffn & 0x10000000000000000n, 0x10000000000000000n, 98 "-0xffffffffffffffffn & 0x10000000000000000n === 0x10000000000000000n"); 99 assert.sameValue( 100 0x10000000000000000n & -0xffffffffffffffffn, 0x10000000000000000n, 101 "0x10000000000000000n & -0xffffffffffffffffn === 0x10000000000000000n"); 102 assert.sameValue( 103 -0xffffffffffffffffffffffffn & 0x10000000000000000n, 0n, 104 "-0xffffffffffffffffffffffffn & 0x10000000000000000n === 0n"); 105 assert.sameValue( 106 0x10000000000000000n & -0xffffffffffffffffffffffffn, 0n, 107 "0x10000000000000000n & -0xffffffffffffffffffffffffn === 0n"); 108 109 reportCompare(0, 0);