Abs returns absolute value using binary operation Principle of operation: 1) Get the mask by right shift by the base 2) Base is the size of an integer variable in bits, for example, for int32 it will be 32, for int64 it will be 64 3) For negative numbers, above step sets mask as 1 1 1 1 1 1 1 1 and
(base, n int)
| 13 | // 4) Add the mask to the given number. |
| 14 | // 5) XOR of mask + n and mask gives the absolute value. |
| 15 | func Abs(base, n int) int { |
| 16 | m := n >> (base - 1) |
| 17 | return (n + m) ^ m |
| 18 | } |
no outgoing calls