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;
carry = (n1 & n2) << 1;
sum = n1 ^ n2;
if (sum & carry)
{
return add(sum, carry);
}
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
. - -, .