Re: Re: Off topic 'C' question

From: Louis-David Mitterrand <cunctator(at)apartia(dot)ch>
To: Mike Mascari <mascarm(at)mascari(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Re: Off topic 'C' question
Date: 2000-08-12 06:50:54
Message-ID: 20000812085054.B4814@styx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Aug 11, 2000 at 11:18:23PM +0200, Louis-David Mitterrand wrote:
> On Sat, Jul 29, 2000 at 09:38:33PM -0400, Mike Mascari wrote:
> > I have a quick question. What is the quickest way to determine
> > the next highest power of two which is greater than a given
> > integer in 'C'. For example, given the number 7, I would like to
> > return 8. Given the number 13, I would like to return 16, etc. Is
> > there a gem to do this without shifting a bit value from 1 left
> > up to a maximum of 32 (or 64) iterations?
>
> You could use:
>
> pow(x, ((int)(log(x)/log(2)) + 1));

Sorry the correct way is:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv) {
int r = atoi(argv[1]);
printf("result is %g\n", pow(2, (int)((log(r)/log(2)) + 1)));
return 0;
}

This works for any power, simply replace 2 by the desired exponent.

--
Louis-David Mitterrand - ldm(at)apartia(dot)org - http://www.apartia.org

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Philip Warner 2000-08-12 07:46:53 Re: [HACKERS] Optimizer confusion?
Previous Message Tom Lane 2000-08-12 06:15:07 Re: [HACKERS] Optimizer confusion?