Skip to content

Instantly share code, notes, and snippets.

@guilleiguaran
Created July 29, 2015 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilleiguaran/8d5fd1a38c18e0ea4558 to your computer and use it in GitHub Desktop.
Save guilleiguaran/8d5fd1a38c18e0ea4558 to your computer and use it in GitHub Desktop.
int log(int x)
//@requires x >= 1;
//@ensures \result >= 0;
//@ensures (1 << \result) <= x;
{ int r = 0;
int y = x;
while (y > 1)
//@loop_invariant y >= 1 && r >= 0;
//@loop_invariant y * (1 << r) <= x;
{
y = y / 2;
//@assert r < x;
r = r + 1;
}
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment