Open EdwardZZZ opened 5 years ago
[...Array(32)].map((_, i) => `${i}, ${Math.pow(2, i)}, ${1<<i}`).join('\n');
toBytes64LE(value: number | string): number[] {
if (value >= 0 && value < 128) return [+value];
const buf = [];
const longValue = Long.fromValue(value);
let r = longValue;
while (buf.length < 9) {
r = longValue.shiftRightUnsigned(7 * buf.length);
if ((r.gt(0x7f) || longValue.isNegative()) && buf.length < 8) {
buf.push(r.and(0x7f).or(0x80).toInt());
} else {
buf.push(r.toInt());
break;
}
}
return buf;
}
readVarInt64(): string {
const [, , len] = Range[64];
let i = 0;
let b = this.buffer[this.pos + i++];
let result = Long.fromValue(b & 0x7f);
while (i < len + 1) {
if ((b & 0x80) !== 0) {
b = this.buffer[this.pos + i++];
if (i === 9) {
result = result.or(Long.fromValue(b).shiftLeft(56));
} else {
result = result.or(Long.fromValue(b & 0x7f).shiftLeft(7 * (i - 1)));
}
} else {
break;
}
}
this.pos += i;
return result.toString();
}
工作几年就早就忘记了位运算,最近用的比较多,写一下基本的加减乘除位运算