我在上面找到了以下简短而棘手的代码
双按位NOT(~~) - James Padolsey
http://james.padolsey.com/javascript/double-bitwise-not/
网络反思:JavaScript中的两个简单技巧(旧的,但总是有用的)
http://webreflection.blogspot.com/2008/06/two-simple-tricks-in-javascript-olds.html
双按位而不是
Math.round(v)
=== ~~v
Math.floor(v)
=== ~~v
(如果v> 0)
isNaN(Number(v)) ? 0 : Number(v)
=== ~~v
(如果v不浮动)
双不
Boolean(v)
=== !!v
(!Boolean(v)
=== !v
)
按位移位
Math.round(v / 2)
=== v >> 1
Math.round(v)
=== v >> 0
单个按位而不是
a.indexOf(v) !== -1
=== ~a.indexOf(v)
javascript中是否有更多简短或棘手的代码?
答案 0 :(得分:4)
这些“技巧”并非特定于Javascript。 在Google上进行简单搜索会返回一些提供相似技巧的网页。
http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html
http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/