1234567891011121314 |
- import (
- "fmt"
- )
- func rangeBitwiseAnd(m int, n int) int {
- bits := uint(0)
- for i := n; 0 < i; i, bits = i>>1, bits+1 {
- }
- and := 0
- for mask := 1 << (bits - 1); 1 <= mask && (m&mask) == (n&mask); mask >>= 1 {
- and |= (m & mask) // Scan from the highest bit, just like 'longest common prefix'
- }
- return and
- }
|