TDLR; I don't know if there is already an existing algorithm somewhere, the aim of this script is to gain knowledge and basic experience on manipulating binary and bits.
Use this command to run the script: node approx_quotient_via_bitshift.js
or npm start
Getting the Approximate Quotient of a certain number using "Bit-shifting" is faster that simple iteration. It is already proven that you can get the approximate value of a number divided by 2 by converting the value to binary and shifting the bits to the right by 1. You can also expand this rule to any number divided by any "power of 2 number" e.g 2, 4, 8 etc. by shifting the bits to the right by 1, 2, 3 etc. respectively. But the approximation is only limited to divisors that are "power of 2 number".
Thus to solve this limitation, I come up with an algorithm that will able to handle any divisor value.
- Process the division by means of bit shifting both the
divisor
anddivident
until thedivident
is 1 or 0. - If the shifted
divisor
is less than or equal to 0, it means that thedivisor
is less than thedivident
and thus will result to a decimal value, so thequotient
is 0. - Next, get the difference between the current calculated
quotient
and the currentdivisor
, we can call thisquotientDifference
. If thequotientDifference
is less than thedivident
, return the currentquotient
as the approximate value of the division, if thequotientDifference
is equal to thedivident
, we return the currentquotient
- 1 as the approximate value of the division, else we continue to the next step. - finally, use the
quotientDifference
as the newdivisor
for the next iteration of the process, and subtract the result of it to the currentquotient
until thequotientDifference
is equal or less than thedivident
.