[prev in list] [next in list] [prev in thread] [next in thread]
List: gmp-devel
Subject: mpz_remove_ui
From: kha () treskal ! com (Karl Hasselstrom)
Date: 2003-07-13 19:46:40
Message-ID: 20030713194640.GA14015 () treskal ! com
[Download RAW message or body]
On 2003-07-12 11:04:52 -0500, Andrew Friedley (CNS Student Support)
wrote:
> A quick glance at the docs didnt show me anything, but is there a
> quick way to determine if a number is a power of two?
The positive integer x is a power of two iff x & (x-1) == 0. (If x is
indeed a power of two, it will have exactly one set bit; (x-1) will
have that bit unset, so their & will be all zero. If x is not a power
of two, x and (x-1) will have the same most significant set bit, which
will be preserved by the &. Note that this algorithm considers 0 to be
a power of two.)
--
Karl Hasselström, kha@treskal.com
www.treskal.com/kalle
[Attachment #3 (application/pgp-signature)]
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic