Solved

64-bit power of 2 calculation

Posted on 2007-11-25
6
1,060 Views
Last Modified: 2012-08-13
I found online a nice algorithm for calculating the next highest power of 2.  However, the algorithm takes a 32-bit integer.  It seems to work just as well on 64-bit integers, but I'm not sure if it's guaranteed to always work.  Will the following algorithm always return a 64-bit integer that is the next greatest power of 2?

uint64_t nextp2(uint64_t n)
{
      --n;
       n |= n >> 16;
       n |= n >> 8;
       n |= n >> 4;
       n |= n >> 2;
       n |= n >> 1;
       ++n;
       return n;
}
0
Comment
Question by:chsalvia
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
the order is important, it should be:

n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n |= n >> 32;
n++;
0
 

Author Comment

by:chsalvia
Comment Utility
That doesn't seem to work.

For example, that yields 0 for the value 100
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
Comment Utility
Using my calculator:

n--;    // gives 99
n |= n >> 1;   // gives 115
n |= n >> 2;   // gives 127
n |= n >> 4;   // gives 127  
n |= n >> 8;   // gives 127
n |= n >> 16;   // gives 127
n |= n >> 32;   // gives 127
n++;  // gives 128

so, it works perfectly with 100


0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:chsalvia
Comment Utility
You're right.  I was screwing up the return value.  Thanks.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
The order is not important
       n |= n >> 16;
       n |= n >> 8;
       n |= n >> 4;
       n |= n >> 2;
       n |= n >> 1;
       n |= n >> 32;
works just as well
but if the number is already a power of two, you get the same number back, not the "next" power of two
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> The order is not important

Indeed. You just needed to add :

       n |= n >> 32;

to make it 64bit integer compatible.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now