JavaScript未使用按位运算符??这是我的代码javascript代码
function subtract()
{
var n1 = parseInt(document.getElementById('fvalue').value);
var n2 = parseInt(document.getElementById('svalue').value);
var x1, x2;
while(n2 != 0)
{
var brw = (~n2)&n2;
n1= n1^n2;
n2 = brw<<1;
}
document.write("Result is : " + n1);
}
答案 0 :(得分:0)
这是在javascript中添加和减去2个数字的简单示例:
console.log(add(2,4));
console.log(subtract(5,4));
function add( a, b)
{
var x;
x = a^b;
while(a&b)
{
b = ((a&b)<<1);
a = x;
x = a^b;
//b=(a^b);
}
return x;
}
function subtract( x, y)
{
if (y == 0)
return x;
return subtract(x ^ y, (~x & y) << 1);
}