Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added is-even-bitwise.md #1953

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions snippets/python/s/is-even-bitwise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
title: Bitwise even number
type: snippet
language: python
tags: [bitwise]
cover: interior-4
dateModified: 2023‐06‐18T05:24:48+00:00
---

Check if the given number is even.

- Checks if the given number is even using the bitwise AND (`&`) operator.
- Actually compares the binary form of number with 1.
- In binary form, even numbers have 0 as last digit, whereas odd numbers have 1 as last digit.
- Returns **true** if the number is even. Otherwise, returns **false**.

```py
def is_even_bitwise(number):
'''
Code snippet to check if a number is an even number using bitwise AND operator.
Arguments:
number (int): The number to be checked.

Returns:
bool: True if the number is even, False if the number is odd.
'''
# Check for negative number
if number < 0 :
number = -number # To make sure the number is never negative.
return (number & 1) == 0
```

```py
is_even_bitwise(2) # true
is_even_bitwise(3) # false
```