Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 268747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:43:52+00:00 2026-05-11T23:43:52+00:00

To check for odd and even integer, is the lowest bit checking more efficient

  • 0

To check for odd and even integer, is the lowest bit checking more efficient than using the modulo?

>>> def isodd(num):
        return num & 1 and True or False

>>> isodd(10)
False
>>> isodd(9)
True
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T23:43:53+00:00Added an answer on May 11, 2026 at 11:43 pm

    Yep. The timeit module in the standard library is how you check on those things. E.g:

    $ python -m timeit -s 'def isodd(x): x & 1' 'isodd(9)'
    1000000 loops, best of 3: 0.446 usec per loop
    $ python -m timeit -s 'def isodd(x): x & 1' 'isodd(10)'
    1000000 loops, best of 3: 0.443 usec per loop
    $ python -m timeit -s 'def isodd(x): x % 2' 'isodd(9)'
    1000000 loops, best of 3: 0.461 usec per loop
    $ python -m timeit -s 'def isodd(x): x % 2' 'isodd(10)'
    1000000 loops, best of 3: 0.453 usec per loop
    

    As you see, on my (first-day==old==slow;-) Macbook Air, the & solution is repeatably between 7 and 18 nanoseconds faster than the % solution.

    timeit not only tells you what’s faster, but by how much (just run the tests a few times), which usually shows how supremely UNimportant it is (do you really care about 10 nanoseconds’ difference, when the overhead of calling the function is around 400?!-)…

    Convincing programmers that micro-optimizations are essentially irrelevant has proven to be an impossible task — even though it’s been 35 years (over which computers have gotten orders of magnitude faster!) since Knuth wrote

    We should forget about small
    efficiencies, say about 97% of the
    time: premature optimization is the
    root of all evil.

    which as he explained is a quote from an even older statement from Hoare. I guess everybody’s totally convinced that THEIR case falls in the remaining 3%!

    So instead of endlessly repeating "it doesn’t matter", we (Tim Peters in particular deserves the honors there) put in the standard Python library module timeit, that makes it trivially easy to measure such micro-benchmarks and thereby lets at least some programmers convince themselves that, hmmm, this case DOES fall in the 97% group!-)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 130k
  • Answers 130k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer OneNote 2007 has more options, but OneNote 2003 only supports… May 12, 2026 at 6:01 am
  • Editorial Team
    Editorial Team added an answer BoxComponent doesn't have the ability to contain child items, if… May 12, 2026 at 6:01 am
  • Editorial Team
    Editorial Team added an answer May be this will be a good solution: taking in… May 12, 2026 at 6:01 am

Related Questions

The way DUnit normally works is you write some published methods, and DUnit runs
Within the gridview I have, I'm trying to make it so when the user
For fun, I've been toying around with writing a load balancer in python and
I'm playing with LINQ-To-SQL in .NET 3.5 and have built some classes based on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.