Last active
December 30, 2024 03:49
-
-
Save GabrielModog/cd4ef6f7a9d49cd25574ebdfaa62c060 to your computer and use it in GitHub Desktop.
Revisions
-
GabrielModog revised this gist
Dec 30, 2024 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,12 +8,12 @@ class BinaryMath { } static sub(a, b) { let barrow = (~a) & b b = a ^ b barrow = barrow << 1 return barrow === 0 ? b : this.sum(barrow, b) } static mul(a, b) { -
GabrielModog created this gist
Nov 9, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ class BinaryMath { static sum(a, b) { let carry = (a & b) carry = carry << 1 return carry === 0 ? b : this.sum(carry, b) } static sub(a, b) { let carry = (~a) & b b = a ^ b carry = carry << 1 return carry === 0 ? b : this.sum(carry, b) } static mul(a, b) { let carry = 0 while(b > 0) { if(b & 1) { carry = this.sum(carry, a) } a <<= 1 b >>= 1 } return carry } static div(a, b) { let quotient = 0 let remainder = a while(remainder >= b) { let shift = 0 while(remainder >= (b << shift)) { shift++ } shift-- quotient |= (1 << shift) remainder = this.subtract(remainder, b << shift) } return quotient } }