Test an Integer Value is a Power of 2

The following code checks whether an integer value is a power of 2:

inline bool IsPowerOf2(int value) {
  return (value & (value-1)) == 0;
}

I got this trick from the fast C++ trie code of Jeff Koftinoff. Jeff use the following code to do above check at compile time:

template <bool test_condition>
struct trie_map_compile_time_test {
  trie_map_compile_time_test()
  {
    switch(false)
      {
      case false:
      case test_condition:
        break;
      }
  }
};

template <unsigned int VALUE>
struct trie_map_test_for_power_of_two
  :  private trie_map_compile_time_test<((VALUE & (VALUE-1))==0)> {
};

If VALUE is not a power-of-2, trie_map_compile_time_test makes the compiler complain that two switch labels are both “false”.