BigIngeter: new LeadingZeroCount helper method
authortheraot <theraot@gmail.com>
Tue, 4 Mar 2014 16:12:41 +0000 (11:12 -0500)
committertheraot <theraot@gmail.com>
Tue, 4 Mar 2014 16:12:41 +0000 (11:12 -0500)
- The new helper method LeadingZeroCount returns the number of bits
  that are set to 0 at the start of an uint.

mcs/class/System.Numerics/System.Numerics/BigInteger.cs

index e52cd09431013fc55357897455ac1364f3f97bb0..be2289c4969f3b5a3c61a3e95770a1c9de71ec66 100644 (file)
@@ -344,6 +344,16 @@ namespace System.Numerics {
                        return (int)(x & 0x0000003F);
                }
 
+               static int LeadingZeroCount(uint value)
+               {
+                       value |= value >> 1;
+                       value |= value >> 2;
+                       value |= value >> 4;
+                       value |= value >> 8;
+                       value |= value >> 16;
+                       return 32 - PopulationCount (value); // 32 = bits in uint
+               }
+
                public bool IsPowerOfTwo {
                        get {
                                bool foundBit = false;