Check number is even or odd using bitwise operator Python

# The AND bitwise operator ‘&’ gives 1 when both bits are 1 else 0. An even number will have bit #  as 0 while odd number will have bit as 1. So if we ‘&’ with 1 (…0001) then for even number it will # be 0 and odd number will be 1

def checkOddEven(element):

    if (element & 1) == 0:

        return ‘Number is even’

    else:

        return‘Number is odd’

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s