非数位操作 [英] Bitwise operations on non numbers

查看:136
本文介绍了非数位操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知何故,JavaScript的使位运算 NaN的^ 1 无限^ 1 ,甚至'A'^ 1 (所有的计算结果为 1 )。

Somehow, JavaScript makes sense of the bitwise operations NaN ^ 1, Infinity ^ 1 and even 'a' ^ 1 (all evaluate to 1).

什么是对非数字执政位运算符的规则?为什么所有上面的例子计算为 1

What are the rules governing bitwise operators on non numbers? Why do all the examples above evaluate to 1?

推荐答案

按照 ES5规范时,做位运算,所有的操作数转换为的 ToInt32 (这首先调用的 ToNumber 。如果值为 NaN的无限,它转换为 0 )。

According to the ES5 spec, when doing bitwise operations, all operands are converted to ToInt32 (which first calls ToNumber. If the value is NaN or Infinity, it's converted to 0).

这样: NaN的^ 1 => 0 XOR 1 => 1

Thus: NaN ^ 1 => 0 XOR 1 => 1

这篇关于非数位操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆