MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / IsPowerOfTwoLeftShift

Function IsPowerOfTwoLeftShift

math/binary/checkisnumberpoweroftwo.go:30–37  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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
30func 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}

Calls

no outgoing calls