Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Best way to extract n from 2^n (n=0..63)?

Author: Alessandro Damiani

Date: 04:56:01 09/13/99

Go up one level in this thread


On September 13, 1999 at 04:48:40, stefan wrote:

>Thank you
>stefan plenkner

Given: X = 2^n, 0<=n<64.

With math:

 n = log(X)/log(2)


With linear search:

postcondition: X = 2^n & 0<=n<64

[[
  {(Exist n: 0<=n<64: X = 2^n)}
  n, y:= 0, 1;
  do y#X ->
    n, y:= n+1, 2*y
  od
]]

or in C:

{
  n= 0; y= 1;
  while (y<X) {
    n++; y+= y;
  }
}

You could use binary search, but there are only 64 elements.

Alessandro



This page took 0.01 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.