简体   繁体   English

为什么这个 ['3'|0 + 1] 按位不起作用?

[英]why does this ['3'|0 + 1] bitwise doesn't work?

it seems this only work on "even" numbers:似乎这只适用于“偶数”数字:

"8"|0 + 1   // 9
"3"|0 + 1   // 3
("3"|0) + 1 // 4

but "3"|0 translates to the Integer 3 ."3"|0转换为 Integer 3

so what's going on here?那么这里发生了什么?
why does the second example acts like this in JS?为什么第二个示例在 JS 中会这样?

The precedence of + is higher than |. + 的优先级高于 |。

So it's parsing like this:所以解析是这样的:

"8" | (0 + 1) = "8" | 1 = 9
"3" | (0 + 1) = "3" | 1 = 3

+ has higher precedence than | +的优先级高于| so you need to put the brackets in to get the order of evaluation that you want.所以你需要把括号放进去以获得你想要的评估顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM