Notes icon indicating copy to clipboard operation
Notes copied to clipboard

用"n>>1"代替Math.floor(n/2)

Open any86 opened this issue 4 years ago • 0 comments

5>>1 // 1
// 等同于
Math.floor(5/2)

位运算">>"的作用

  1. 把5的变成2进制: 101
  2. 最高位置向后推1位, 也就是101 => 10(删除101最后的1)
  3. 10的10进制就是2.

进而

5 >> 2 //  Math.floor(5 / (2*2))

5 >> 3 // Math.floor(5 / (2*2*2))

any86 avatar Aug 03 '20 06:08 any86