4.6.40.2 Macros

  • #define IsPowerOfTwo(number) ((0u != (number)) && (((number) & ((number)-1u)) == 0u))

    Algorithm to detect if a given number is a power of two. A number is a power of two if it has exactly one '1' in its binary representation. This is true if subtracting '1' from the number and doing an AND operation on the result with the number itself returns 0.