Javascript,^(插入符号)操作符做什么?
我有一些JavaScript代码:
<script type="text/javascript"> $(document).ready(function(){ $('#calcular').click(function() { var altura2 = ((($('#ddl_altura').attr("value"))/100)^2); var peso = $('#ddl_peso').attr("value"); var resultado = Math.round(parseFloat(peso / altura2)*100)/100; if (resultado > 0) { $('#resultado').html(resultado); $('#imc').show(); }; }); }); </script>
Javascript中的^符号是什么意思?
^运算符是按位异或运算符。 要将值平方,请使用Math.pow :
var altura2 = Math.pow($('#ddl_altura').attr("value")/100, 2);
^正在执行异或(XOR),例如
6是二进制011是011 ,二进制
6 ^ 3 ,意思是110 XOR 011给出101(5)。
110 since 0 ^ 0 => 0 011 0 ^ 1 => 1 --- 1 ^ 0 => 1 101 1 ^ 1 => 0
Math.pow(x,2)计算x²但是对于平方,您最好使用x*x因为Math.pow使用对数,您会得到更多的近似误差。 ( x² ~ exp(2.log(x)) )
这是按位XOR运算符。
按位异或操作符由插入符号(^)表示,当然,它直接用于二进制forms的数字。 按位XOR不同于按位OR,因为只有当正好有一位的值为1时才返回1。
来源: http : //www.java-samples.com/showtutorial.php? tutorialid= 820
它被称为按位XOR。 让我解释一下:
你有 :
Decimal Binary 0 0 1 01 2 10 3 11
现在我们想要3^2= ? 那么我们有11^10=?
11 10 --- 01 ---
所以Decimal中的11^10=01 01是1 。
所以我们可以说3^2=1;