mpz_remove_ui

Karl Hasselstrom kha@treskal.com
Sun, 13 Jul 2003 21:46:40 +0200


--IJpNTDwzlM2Ie8A6
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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) =3D=3D 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.)

--=20
Karl Hasselstr=F6m, kha@treskal.com
      www.treskal.com/kalle

--IJpNTDwzlM2Ie8A6
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/EbcgN7iPzXSoOQoRAitoAJoDdIpB1NAeQuaI4scjN6yKTjc+sgCfTWj8
jhaNKbKmGaV/m5U88+GH9iw=
=+HKJ
-----END PGP SIGNATURE-----

--IJpNTDwzlM2Ie8A6--