zigzag maps a consecutive sequence of integers to a zig-zag sequence. [0 1 2 3 4 5 ...] => [0 -1 +1 -2 +2 ...]
(x int)
| 395 | // |
| 396 | // [0 1 2 3 4 5 ...] => [0 -1 +1 -2 +2 ...] |
| 397 | func zigzag(x int) int { |
| 398 | if x&1 != 0 { |
| 399 | x = ^x |
| 400 | } |
| 401 | return x >> 1 |
| 402 | } |