zhanhongtao / blog

Blog for 91885076(QQ群)
http://github.com/zhanhongtao/blog/issues
24 stars 7 forks source link

lowbit #264

Closed zhanhongtao closed 7 years ago

zhanhongtao commented 7 years ago

定义

Lowbit函数,返回参数转为二进制后, 最后一个1的位置所代表的数值.

function lowbit(n) {
  return n & (-n)
}

demo

/*
12(10) -> 1100(2) -> 100(2) -> 4(10)  // (12).toString(2)
8(10) -> 1000(2) -> 1000(2) -> 8(10)  // (8).toString(2)
*/
lowbit(12) // 4
lowbit(8) // 8