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 864963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:33:55+00:00 2026-05-15T09:33:55+00:00

I want to calculate 2 n -1 for a 64bit integer value. What I

  • 0

I want to calculate 2n-1 for a 64bit integer value.
What I currently do is this

for(i=0; i<n; i++) r|=1<<i;

and I wonder if there is more elegant way to do it.
The line is in an inner loop, so I need it to be fast.

I thought of

  r=(1ULL<<n)-1;

but it doesn’t work for n=64, because << is only defined
for values of n up to 63.


EDIT:
Thanks for all your answers and comments.
Here is a little table with the solutions that I tried and liked best.
Second column is time in seconds of my (completely unscientific) benchmark.

    
r=N2MINUSONE_LUT[n];            3.9 lookup table = fastest, answer by aviraldg
r =n?~0ull>>(64 - n):0ull;      5.9 fastest without LUT, comment by Christoph
r=(1ULL<<n)-1;                  5.9 Obvious but WRONG!   
r =(n==64)?-1:(1ULL<<n)-1;      7.0 Short, clear and quite fast, answer by Gabe
r=((1ULL<<(n/2))<<((n+1)/2))-1; 8.2 Nice, w/o spec. case, answer by drawnonward
r=(1ULL<<n-1)+((1ULL<<n-1)-1);  9.2 Nice, w/o spec. case, answer by David Lively
r=pow(2, n)-1;               99.0 Just for comparison
for(i=0; i<n; i++) r|=1<<i;   123.7 My original solution = lame

I accepted

r =n?~0ull>>(64 - n):0ull;

as answer because it’s in my opinion the most elegant solution.
It was Christoph who came up with it at first, but unfortunately he only posted it in a
comment. Jens Gustedt added a really nice rationale, so I accept his answer instead. Because I liked Aviral Dasgupta’s lookup table solution it got 50 reputation points via a bounty.

  • 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-15T09:33:55+00:00Added an answer on May 15, 2026 at 9:33 am

    I like aviraldg answer best.
    Just to get rid of the `ULL’ stuff etc in C99 I would do

    static inline uint64_t n2minusone(unsigned n) {
       return n ? (~(uint64_t)0) >> (64u - n) : 0;
    }
    

    To see that this is valid

    • an uint64_t is guaranteed to have a width of exactly 64 bit
    • the bit negation of that `zero of type uint64_t’ has thus exactly
      64 one bits
    • right shift of an unsigned value is guaranteed to be a logical
      shift, so everything is filled with zeros from the left
    • shift with a value equal or greater to the width is undefined, so
      yes you have to do at least one conditional to be sure of your result
    • an inline function (or alternatively a cast to uint64_t if you
      prefer) makes this type safe; an unsigned long long may
      well be an 128 bit wide value in the future
    • a static inline function should be seamlessly
      inlined in the caller without any overhead
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to calculate the time span between 2 times which I saved in
I want to calculate what is $x percentage of a $total. $x could be
I want to calculate the age of a person given the date of birth
I want to loop over the contents of a text file and do a
I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
I have a class that has some properties. And I want something that calculates
I want to call a function (which calculates my version number) when my NSIS
Want to know what the stackoverflow community feels about the various free and non-free
Want my FireFox at work to be in sync with my FireFox at my
I want to create a Java application bundle for Mac without using Mac. According

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.