Added bitwise operations to BigNumber (#781).
This commit is contained in:
parent
220e0710a9
commit
7498c18235
@ -89,17 +89,62 @@ export class BigNumber implements Hexable {
|
||||
}
|
||||
|
||||
mod(other: BigNumberish): BigNumber {
|
||||
return toBigNumber(toBN(this).mod(toBN(other)));
|
||||
const value = toBN(other);
|
||||
if (value.isNeg()) {
|
||||
throwFault("cannot modulo negative values", "mod");
|
||||
}
|
||||
return toBigNumber(toBN(this).umod(value));
|
||||
}
|
||||
|
||||
pow(other: BigNumberish): BigNumber {
|
||||
return toBigNumber(toBN(this).pow(toBN(other)));
|
||||
}
|
||||
|
||||
maskn(value: number): BigNumber {
|
||||
and(other: BigNumberish): BigNumber {
|
||||
const value = toBN(other);
|
||||
if (this.isNegative() || value.isNeg()) {
|
||||
throwFault("cannot 'and' negative values", "and");
|
||||
}
|
||||
return toBigNumber(toBN(this).and(value));
|
||||
}
|
||||
|
||||
or(other: BigNumberish): BigNumber {
|
||||
const value = toBN(other);
|
||||
if (this.isNegative() || value.isNeg()) {
|
||||
throwFault("cannot 'or' negative values", "or");
|
||||
}
|
||||
return toBigNumber(toBN(this).or(value));
|
||||
}
|
||||
|
||||
xor(other: BigNumberish): BigNumber {
|
||||
const value = toBN(other);
|
||||
if (this.isNegative() || value.isNeg()) {
|
||||
throwFault("cannot 'xor' negative values", "xor");
|
||||
}
|
||||
return toBigNumber(toBN(this).xor(value));
|
||||
}
|
||||
|
||||
mask(value: number): BigNumber {
|
||||
if (this.isNegative() || value < 0) {
|
||||
throwFault("cannot mask negative values", "mask");
|
||||
}
|
||||
return toBigNumber(toBN(this).maskn(value));
|
||||
}
|
||||
|
||||
shl(value: number): BigNumber {
|
||||
if (this.isNegative() || value < 0) {
|
||||
throwFault("cannot shift negative values", "shl");
|
||||
}
|
||||
return toBigNumber(toBN(this).shln(value));
|
||||
}
|
||||
|
||||
shr(value: number): BigNumber {
|
||||
if (this.isNegative() || value < 0) {
|
||||
throwFault("cannot shift negative values", "shr");
|
||||
}
|
||||
return toBigNumber(toBN(this).shrn(value));
|
||||
}
|
||||
|
||||
eq(other: BigNumberish): boolean {
|
||||
return toBN(this).eq(toBN(other));
|
||||
}
|
||||
@ -120,6 +165,10 @@ export class BigNumber implements Hexable {
|
||||
return toBN(this).gte(toBN(other));
|
||||
}
|
||||
|
||||
isNegative(): boolean {
|
||||
return (this._hex[0] === "-");
|
||||
}
|
||||
|
||||
isZero(): boolean {
|
||||
return toBN(this).isZero();
|
||||
}
|
||||
|
10
packages/bignumber/thirdparty.d.ts
vendored
10
packages/bignumber/thirdparty.d.ts
vendored
@ -9,7 +9,7 @@ declare module "bn.js" {
|
||||
mul(other: BN): BN;
|
||||
|
||||
pow(other: BN): BN;
|
||||
maskn(other: number): BN;
|
||||
umod(other: BN): BN;
|
||||
|
||||
eq(other: BN): boolean;
|
||||
lt(other: BN): boolean;
|
||||
@ -17,11 +17,19 @@ declare module "bn.js" {
|
||||
gt(other: BN): boolean;
|
||||
gte(other: BN): boolean;
|
||||
|
||||
isNeg(): boolean;
|
||||
isZero(): boolean;
|
||||
|
||||
toTwos(other: number): BN;
|
||||
fromTwos(other: number): BN;
|
||||
|
||||
or(other: BN): BN;
|
||||
and(other: BN): BN;
|
||||
xor(other: BN): BN;
|
||||
shln(other: number): BN;
|
||||
shrn(other: number): BN;
|
||||
maskn(other: number): BN;
|
||||
|
||||
toString(radix: number): string;
|
||||
toNumber(): number;
|
||||
toArray(endian: string, width: number): Uint8Array;
|
||||
|
Loading…
Reference in New Issue
Block a user