EdwardZZZ / articles

工作点滴记录
2 stars 0 forks source link

bigint #42

Open EdwardZZZ opened 6 years ago

EdwardZZZ commented 6 years ago
function toArray(n) {
    const arr = [];
    let str = n;

    // return str.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,').split(',').reverse();

    while (n.length > 7) {
        arr.push(n.slice(-7));
        n = n.slice(0, n.length - 7);
    }
    if (n) { arr.push(n); }
    return arr;
}

function plus(n1, n2) {
    const arr1 = n1.split('').reverse();
    const arr2 = n2.split('').reverse();

    const resultArr = [];
    let ten = 0;
    for (let i = 0; i < Math.max(arr2.length, arr1.length); i++) {
        const a1 = arr1[i] || 0;
        const a2 = arr2[i] || 0;

        if (a1 && a2) {
            const n = a1 + a2;
            if (n > 9) {
                resultArr.push(n % 10);
                ten = 1;
            } else {
                resultArr.push(n);
            }
        } else {
            resultArr.push(a1 + a2 + ten);
        }
    }

    return resultArr.reverse().join('');
}

function times(n1, n2) {
    const arr1 = toArray(n1);
    const arr2 = toArray(n2);

    const multyArr = [];

    const newArr = [];
    for (let i = 0; i < arr1.length; i++) {
        const a1 = arr1[i];

        for (let j = 0; j < arr2.length; j++) {
            const a2 = arr2[j];

            multyArr[i + j] = multyArr[i + j] || [];
            multyArr[i + j].push(a1 * a2);
        }
    }

    const resultArr = [];
    const re = multyArr.reduce((prev, curr) => {
        const total = prev + curr.reduce((p, c) => p + c, 0);

        resultArr.push(total % 1e7);

        return Math.floor(total / 1e7);
    }, 0);
    re && resultArr.push(re);

    return resultArr;
}

function pow(n, m) {
    let result = n;
    while (m) {
        m--;
        result = times(result, n);
    }
    return result;
}

function toArr(n, base) {
    const nArr = toArray(n);
    console.log(nArr);

    let _nArr = nArr;
    const resultArr = [];
    while(true) {
        const newArr = [];
        const re = _nArr.reduceRight((prev, curr) => {
            const total = prev * 1e7 + curr

            newArr.push(Math.floor(total / base))

            return total % base;
        }, 0);

        _nArr = newArr.reverse();
        resultArr.push(re);

        if (!+_nArr.join('')) break;
    }

    return resultArr;
}

function division(n, div) {
    const nArr = toArray(n);

    const resultArr = [];
    nArr.reduceRight((prev, curr) => {
        const total = prev * 1e7 + curr

        resultArr.push(Math.floor(total / div))

        return total % div;
    }, 0);

    return resultArr;
}

// console.log(toArray('1234567890'))
// console.log(plus('23000', '456'));
// console.log(division('2000000000', 123));
// console.log(times('1111111111', '2222222222'));
// console.log('pow==>', pow('2', '4'));
// // [ 171, 84, 169, 140, 235, 31, 10, 210 ]
console.log(toArr('12345678901234567890', 256));
EdwardZZZ commented 4 years ago

// const n = 9223372036854775807n;
const n = 1223372036854775907n;

const high = 284838498;
const low = -991952797;

// console.log(n.toString().length);
// const buff = Buffer.alloc(8);
// buff.writeBigInt64BE(n);

// console.log(...buff);

// console.log(buff.readBigInt64BE());
// console.log(buff.readInt32BE(0));
// console.log(buff.readInt32BE(4));

const buff = Buffer.alloc(8);
buff.writeInt32BE(high);
buff.writeInt32BE(low, 4);
console.log(...buff);
console.log(buff.readBigInt64BE());
EdwardZZZ commented 4 years ago
const int2UintBytes = (n) => {
    const bytes = [];
    // const size = 4;
    // for (let i = 0; i < size; i++) {
    //     bytes[i] = (n >> (size - i - 1) * 8)  & 0xff;
    // }
    bytes[0] = n >> 24 & 0xff;
    bytes[1] = n >> 16 & 0xff;
    bytes[2] = n >> 8 & 0xff;
    bytes[3] = n & 0xff;
    return bytes;
}
EdwardZZZ commented 4 years ago
const n = 1n;
BigInt.prototype.toJSON = function () { return this.toString(); }
console.log(JSON.stringify(n));

// const bigIntReplacer = (() => (_, val) => typeof val === 'bigint' ? val.toString() : val)();

// const ref = {
//     b: 2n,
// }
// const obj = {
//     a: 1n,
//     c: ref,
//     d: ref,
// }
// console.log(JSON.stringify(obj, bigIntReplacer));
EdwardZZZ commented 2 years ago

zigzag

function int64ToZigzag(n: bigint) {
    return (n << 1n) ^ (n >> 63n);
}

function zigzagToInt64(n: bigint) {
    return (n >> 1n) ^ -(n & 1n);
}

const arr = [0, -1, 1, -2, 2];
arr.forEach((v) => {
    console.log(v, '\t', int64ToZigzag(BigInt(v)));
});

const res = [1, 2, 3, 4, 5];
res.forEach((v) => {
    console.log(v, '\t', zigzagToInt64(BigInt(v)));
});