Saving floating point and adding bitwise operation in javascript

I am trying to figure out a way to add, subtract, divide and multiply by working with bits.

Some optimization needs to be done in my JavaScript program due to the many calculations running after the event occurred.

Using the code below for reference, I can understand that wrapping holds the & ing value. Then, by executing XOr, which sets the sum of var to bits that do not match in each variable n1 / n2.

Here is my question.) What makes the shift (n1 and n2) <1 by 1? What is the purpose of this? As in the case of XOr, it is obvious that there is no need to do anything else with these bits, because their decimal values ​​are in order, since they are in the sum of var. I can’t imagine what happens with the switch and switch operation.

function add(n1,n2)
{
        var carry, sum;

        // Find out which bits will result in a carry.
        // Those bits will affect the bits directly to
        // the left, so we shall shift one bit.
        carry = (n1 & n2) << 1;

        // In digital electronics, an XOR gate is also known
        // as a quarter adder.  Basically an addition is performed
        // on each individual bit, and the carry is discarded.
        //
        // All I'm doing here is applying the same concept.
        sum = n1 ^ n2;

        // If any bits match in position, then perform the
        // addition on the current sum and the results of
        // the carry.
        if (sum & carry)
        {
                return add(sum, carry);
        }

        // Return the sum.
        else
        {
                return sum ^ carry;
        };
};

The code above works as expected, but it does not return a floating point value. I need the sum to be returned along with the floating point value.

Does anyone have a function that I can use with above that will help me with floating point? Is there a website with a clear explanation of what I'm looking for? I tried looking for the last day, so I can’t find anything to look at.

I got the code above from this resource. http://www.dreamincode.net/code/snippet3015.htm

Thanks in advance!

, 1 - 2.

: = (n1 n2) < < 1; var , n1 n2. , n1 4 n2 4, . , 1, 4 x 2 = 8; 8.

1.) var carry = 00001000 = 8                                 00001000 = 8

2.) carry = 00001000 = 8

8 x 2 = 16 8 + 8 = 16

3.) carry = carry < 1,

4.) 00010000 = 16

. - -, .

+5
2

, , , . IEEE 754, : , , , , 1 () 2 (), ,

(sign is set ? 1 : -1) * (mantissa ^ (exponent - bias))

. , , , , , . , - , , .

, , 2.3 ( ) 4002666666666666, 5.3 4015333333333333. - 4017777777777777, () 5.866666.

, http://www.psc.edu/general/software/packages/ieee/ieee.php, http://babbage.cs.qc.edu/IEEE-754/ http://www.binaryconvert.com/convert_double.html .

, , . , ( ), , , IEEE754 . , @LukeGT, , , , JS-. JS , , , , .

+2

, . , - , Javascript . , , .

x . , , , .

, .

+1

All Articles