Search
 
SCRIPT & CODE EXAMPLE
 

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")
Comment

PREVIOUS NEXT
Code Example
Python :: set focus in last position entry tkinter 
Python :: pandas reverse explode 
Python :: adding multiple items to a list python 
Python :: best python library to download files 
Python :: Print 10 most important features ascending 
Python :: how to check columns with the numerical values 
Python :: Python Tkinter MenuButton Widget Syntax 
Python :: nim game in python 
Python :: how to combine sets using update() Function 
Python :: Copy an Object in Python using = operator 
Python :: Calculate summary statistics across columns 
Python :: how to code fibonacci series in python 
Python :: python denest list of anything 
Python :: Python List Note 
Python :: running setup.py install for rjsmin ... error 
Python :: python pod status phase 
Python :: While Loop Python Range Staying Constant Despite Shrinking List 
Python :: python convert docx to pdf linux 
Python :: How to print specific figure in python 
Python :: what is self in python constructor 
Python :: Matrix Transpose using Nested Loop 
Python :: hash tables in python 
Python :: transform jpg image into array for conv2d 
Python :: in np array how to make element as 1 if it exceeds the threshold 
Python :: draw networkx graph using plt.pause 
Python :: distplot for 2 columns 
Python :: vvm 2020 exam date 
Python :: python convert unicode escape sequence 
Python :: insert python 
Python :: Modifying a set in Python 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =