Created
June 13, 2021 07:59
-
-
Save islandjoe/506495896a42fd87ce047bce6029c6c0 to your computer and use it in GitHub Desktop.
Python odd-even using bitwise operation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The classic code: | |
``` | |
def odd_even(n): | |
if n % 2 == 0: | |
print('even') | |
else: | |
print('odd') | |
odd_even(13) | |
-> 'odd' | |
``` | |
Using `&` bitwise operation: | |
``` | |
def odd_even(n): | |
if n & 1: | |
print('odd') | |
else: | |
print('even') | |
odd_even(13) | |
-> 'odd' | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment