Open EdwardZZZ opened 6 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());
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;
}
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));
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)));
});