Zayko in the Net

Personal blog of Vitaly Zayko


Is Power of 2? (C#)

Continue playing with Integers. Here is how to determine if a number is power of 2 or not:

 /// <summary>
 /// Checks if an integer is power of 2
 /// </summary>
 /// <param name="i">An int number to check</param>
 /// <returns>true if i is power of 2, false if otherwise</returns>
 public static bool IsPower2(int i)
     return (i != 0) && ((i & -i) == i);

Add comment