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

  • Home
  • SEARCH
  • 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 7519979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:59:22+00:00 2026-05-30T01:59:22+00:00

I am given a large integer a , and a (relatively small) integer n

  • 0

I am given a large integer a, and a (relatively small) integer n.

What is the fastest way to get the nth bit (from the right) of the binary representation of a using native Python?

  • 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-30T01:59:23+00:00Added an answer on May 30, 2026 at 1:59 am

    You asked for the fastest way, presumably using a modern version of Python. Modern versions of Python have variable-length ints, and conventional wisdom does’t apply. Shifting a large number is not cheap. Shifting 1 is cheap. Here are some -mtimeit inputs and corresponding outputs. The first is an abbreviation of

    windows command prompt>\python27\python -mtimeit -s"a=10**20;n=3" "(a>>n)&1"
    1000000 loops, best of 3: 0.238 usec per loop
    
    -s"a=10**20;n=3" "(a>>n)&1"
    0.238 usec 
    
    -s"a=10**20;n=3" "not not(a & (1 << n))"
    0.154 usec 
    
    -s"a=10**200;n=3" "(a>>n)&1"
    0.382 usec 
    
    -s"a=10**200;n=3" "not not(a & (1 << n))"
    0.155 usec 
    
    -s"a=10**10;n=3" "(a>>n)&1"
    0.231 usec 
    
    -s"a=10**10;n=3" "not not(a & (1 << n))"
    0.156 usec 
    
    -s"a=10**9;n=3" "(a>>n)&1"
    0.0801 usec
    
    -s"a=10**9;n=3" "not not(a & (1 << n))"
    0.0938 usec
    
    -s"a=2**1000;n=64" "(a>>n)&1"
    0.446 usec
    
    -s"a=2**1000;n=64" "not not(a & (1 << n))"
    0.255 usec
    

    If not not(foo) freaks you out, or you really want an int answer instead of a bool, you can use 1 if foo else 0; it’s only slightly slower.

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

Sidebar

Related Questions

Given a large text block from a WYSIWYG like: Lorem ipsum dolor sit amet,
Given a N-dimensional vector of small integers is there any simple way to map
I have a table containing integer values from 0 to some large number N,
You’re given an array of N 64 bit integers. N may be very large.
I’m trying to determine the relationship of a given compiler’s integer types’ sizes using
Actually, given N a (possibly very large) even integer, I want to find N
Given a large API (an in particular, the Java or J2EE standard libraries), is
Given a large legacy project utilizing ASP.NET, javascript, css, etc, technologies, I was wondering
Suppose I'm given a large dictionary in flat file with 200 million words and
This code works great for generating thumbnails, but when given a very large (100MB+)

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.