From fb7ff4ce74ff3570a46c6fe8a5754a17fcefdfa2 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 9 Aug 2009 17:03:13 +0800 Subject: [PATCH] number theory: integer IsPowerOfTwo Signed-off-by: Christoph Ruegg --- .../NumberTheoryTests/IntegerTheoryTest.cs | 58 +++++++++++++++++++ src/Managed/NumberTheory/IntegerTheory.cs | 20 +++++++ 2 files changed, 78 insertions(+) diff --git a/src/Managed.UnitTests/NumberTheoryTests/IntegerTheoryTest.cs b/src/Managed.UnitTests/NumberTheoryTests/IntegerTheoryTest.cs index 76360979..8741ee6e 100644 --- a/src/Managed.UnitTests/NumberTheoryTests/IntegerTheoryTest.cs +++ b/src/Managed.UnitTests/NumberTheoryTests/IntegerTheoryTest.cs @@ -73,6 +73,64 @@ namespace MathNet.Numerics.UnitTests.NumberTheoryTests Assert.IsFalse(IntegerTheory.IsOdd(Int64.MinValue), "Int64.Min is not odd"); } + [Test] + public void TestIsPowerOfTwo32() + { + for (int i = 2; i < 31; i++) + { + int x = 1 << i; + Assert.IsTrue(IntegerTheory.IsPowerOfTwo(x), x + " (+)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(x - 1), x + "-1 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(x + 1), x + "+1 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x), "-" + x + " (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x + 1), "-" + x + "+1 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x - 1), "-" + x + "-1 (-)"); + } + + Assert.IsTrue(IntegerTheory.IsPowerOfTwo(4), "4 (+)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(3), "3 (-)"); + Assert.IsTrue(IntegerTheory.IsPowerOfTwo(2), "2 (+)"); + Assert.IsTrue(IntegerTheory.IsPowerOfTwo(1), "1 (+)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(0), "0 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-1), "-1 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-2), "-2 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-3), "-3 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-4), "-4 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int32.MinValue), "Int32.MinValue (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int32.MinValue+1), "Int32.MinValue+1 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int32.MaxValue), "Int32.MaxValue (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int32.MaxValue-1), "Int32.MaxValue-1 (-)"); + } + + [Test] + public void TestIsPowerOfTwo64() + { + for (int i = 2; i < 63; i++) + { + long x = ((long)1) << i; + Assert.IsTrue(IntegerTheory.IsPowerOfTwo(x), x + " (+)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(x - 1), x + "-1 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(x + 1), x + "+1 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x), "-" + x + " (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x + 1), "-" + x + "+1 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x - 1), "-" + x + "-1 (-)"); + } + + Assert.IsTrue(IntegerTheory.IsPowerOfTwo((long)4), "4 (+)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)3), "3 (-)"); + Assert.IsTrue(IntegerTheory.IsPowerOfTwo((long)2), "2 (+)"); + Assert.IsTrue(IntegerTheory.IsPowerOfTwo((long)1), "1 (+)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)0), "0 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)-1), "-1 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)-2), "-2 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)-3), "-3 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)-4), "-4 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int64.MinValue), "Int32.MinValue (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int64.MinValue+1), "Int32.MinValue+1 (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int64.MaxValue), "Int32.MaxValue (-)"); + Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int64.MaxValue-1), "Int32.MaxValue-1 (-)"); + } + [Test] public void TestIsPerfectSquare32() { diff --git a/src/Managed/NumberTheory/IntegerTheory.cs b/src/Managed/NumberTheory/IntegerTheory.cs index d92a8207..3130d667 100644 --- a/src/Managed/NumberTheory/IntegerTheory.cs +++ b/src/Managed/NumberTheory/IntegerTheory.cs @@ -75,6 +75,26 @@ namespace MathNet.Numerics.NumberTheory return (number & 0x1) == 0x1; } + /// + /// Find out whether the provided 32 bit integer is a perfect power of two. + /// + /// The number to very whether it's a power of two. + /// True if and only if it is a power of two. + public static bool IsPowerOfTwo(this int number) + { + return number > 0 && (number & (number - 1)) == 0x0; + } + + /// + /// Find out whether the provided 64 bit integer is a perfect power of two. + /// + /// The number to very whether it's a power of two. + /// True if and only if it is a power of two. + public static bool IsPowerOfTwo(this long number) + { + return number > 0 && (number & (number - 1)) == 0x0; + } + /// /// Find out whether the provided 32 bit integer is a perfect square, i.e. a square of an integer. ///