Skip to content

Instantly share code, notes, and snippets.

@islandjoe
Created June 13, 2021 07:59
Show Gist options
  • Save islandjoe/506495896a42fd87ce047bce6029c6c0 to your computer and use it in GitHub Desktop.
Save islandjoe/506495896a42fd87ce047bce6029c6c0 to your computer and use it in GitHub Desktop.
Python odd-even using bitwise operation
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