IsPowerOfTwoLeftShift This function takes advantage of the fact that left shifting a number by 1 is equivalent to multiplying by 2. For example, binary 00000001 when shifted by 3 becomes 00001000, which in decimal system is 8 or = 2 * 2 * 2
(number uint)
| 28 | // by 1 is equivalent to multiplying by 2. For example, binary 00000001 when shifted by 3 becomes 00001000, |
| 29 | // which in decimal system is 8 or = 2 * 2 * 2 |
| 30 | func IsPowerOfTwoLeftShift(number uint) bool { |
| 31 | for p := uint(1); p <= number; p = p << 1 { |
| 32 | if number == p { |
| 33 | return true |
| 34 | } |
| 35 | } |
| 36 | return false |
| 37 | } |
no outgoing calls