From 0b95faaafaf94a1ab176b6bfc67b1243035a32bf Mon Sep 17 00:00:00 2001 From: Arun Kishore Voleti <49097688+ArunKishoreVoleti@users.noreply.github.com> Date: Tue, 14 Oct 2025 15:10:38 +0530 Subject: [PATCH 1/2] Add is_even_using_shift_operator function Introduces a new function to check if a number is even using bitwise shift operators. Includes docstring with explanation and doctest examples. --- bit_manipulation/is_even.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/bit_manipulation/is_even.py b/bit_manipulation/is_even.py index 6f95a1160797..64d638dead1d 100644 --- a/bit_manipulation/is_even.py +++ b/bit_manipulation/is_even.py @@ -30,6 +30,40 @@ def is_even(number: int) -> bool: """ return number & 1 == 0 +def is_even_using_shift_operator(number: int) -> bool: + """ + Returns True if the input integer is even. + + Explanation: + In binary, even numbers end with 0, odd numbers end with 1. + Examples: + 2 -> 10 + 3 -> 11 + 4 -> 100 + 5 -> 101 + + For odd numbers, the last bit is always 1. + Using shift: + (n >> 1) << 1 removes the last bit. + If result equals n, n is even. + + >>> is_even_using_shift_operator(1) + False + >>> is_even_using_shift_operator(4) + True + >>> is_even_using_shift_operator(9) + False + >>> is_even_using_shift_operator(15) + False + >>> is_even_using_shift_operator(40) + True + >>> is_even_using_shift_operator(100) + True + >>> is_even_using_shift_operator(101) + False + """ + return (number >> 1) << 1 == number + if __name__ == "__main__": import doctest From f90c1f819f7c710a9ec06320301ccb8c2e40ff9b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Oct 2025 09:43:25 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/is_even.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bit_manipulation/is_even.py b/bit_manipulation/is_even.py index 64d638dead1d..0f64ccab9bde 100644 --- a/bit_manipulation/is_even.py +++ b/bit_manipulation/is_even.py @@ -30,6 +30,7 @@ def is_even(number: int) -> bool: """ return number & 1 == 0 + def is_even_using_shift_operator(number: int) -> bool: """ Returns True if the input integer is even.