Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

How determine if a number is even or odd using bitwise operator

"""
bitwise operator can be used to compare the Least
Significant Bit (LSB) of a number with the number 1 (0b00000001). Even
numbers have 0 for the LSB (i.e., 2 = 0b00000010), odd numbers have 1 for the
LSB (i.e., 3 = 0b00000011). The not operator is needed to return True for an
even number and False for an odd number.
"""

num = int(input(">"))

def is_even_bitwise(number=num):
    return not (number & 1)

if is_even_bitwise():
    print(f"{num} is an even number")
else:
    print(f"{num} is an odd number")
 
PREVIOUS NEXT
Tagged: #How #determine #number #odd #bitwise #operator
ADD COMMENT
Topic
Name
4+2 =