[Tutor] Power of Two Function

Lazar chaostorm at gmail.com
Sat Sep 1 00:54:27 CEST 2012


I'm fairly new to Python and still learning.

Can someone please explain to me in what way the following function
checks if a number is a power of two? Basically, the second line of
code is what I can't really grasp:

def is_power2(num):
	return num != 0 and ((num & (num - 1)) == 0)

Thank you,

More information about the Tutor mailing list