Skip to content

Commit

Permalink
Added bitwise operations to BigNumber (#781).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Apr 15, 2020
1 parent 220e071 commit 7498c18
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
53 changes: 51 additions & 2 deletions packages/bignumber/src.ts/bignumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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();
}
Expand Down
10 changes: 9 additions & 1 deletion packages/bignumber/thirdparty.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@ 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;
lte(other: BN): boolean;
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;
Expand Down

0 comments on commit 7498c18

Please sign in to comment.