[Mono-dev] BitVector32 patch

Scott Peterson lunchtimemama at gmail.com
Fri Aug 15 23:17:50 EDT 2008


OK, this patch just spruces up the HighestSetBit method and fixes the bug.

Index: class/System/System.Collections.Specialized/ChangeLog
===================================================================
--- class/System/System.Collections.Specialized/ChangeLog    (revision
109665)
+++ class/System/System.Collections.Specialized/ChangeLog    (working copy)
@@ -1,3 +1,10 @@
+2008-08-06  Scott Peterson  <lunchtimemama at gmail.com>
+
+    * BitVector32.cs: Fixed a bug which allowed for invalid sections
+    to be created with CreateSection. Also simlpified HighestSetBit
+    algorithm and got rid ofNumberOfSetBits (using HighestSetBit
+    works just fine).
+
 2008-07-31  Jb Evain  <jbevain at novell.com>

     * StringDictionary.cs: remove ComponentModel bits for NET_2_1.
Index: class/System/System.Collections.Specialized/BitVector32.cs
===================================================================
--- class/System/System.Collections.Specialized/BitVector32.cs    (revision
109665)
+++ class/System/System.Collections.Specialized/BitVector32.cs    (working
copy)
@@ -184,11 +184,11 @@
             if (maxValue < 1)
                 throw new ArgumentException ("maxValue");

-            int bit = HighestSetBit(maxValue) + 1;
+            int bit = HighestSetBit(maxValue);
             int mask = (1 << bit) - 1;
-            int offset = previous.Offset + NumberOfSetBits (previous.Mask);
+            int offset = previous.Offset + HighestSetBit (previous.Mask);

-            if (offset > 32) {
+            if (offset + bit > 32) {
                 throw new ArgumentException ("Sections cannot exceed 32
bits in total");
             }

@@ -227,27 +227,12 @@
         }

         // Private utilities
-        private static int NumberOfSetBits (int i)
+        private static int HighestSetBit (int i)
         {
             int count = 0;
-            for (int bit = 0; bit < 32; bit++) {
-                int mask = 1 << bit;
-                if ((i & mask) != 0)
-                    count++;
-            }
+            while(i >> count != 0)
+                count++;
             return count;
         }
-
-        private static int HighestSetBit (int i)
-        {
-            for (int bit = 31; bit >= 0; bit--) {
-                int mask = 1 << bit;
-                if ((mask & i) != 0) {
-                    return bit;
-                }
-            }
-
-            return -1;
-        }
     }
 }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080816/84f99a41/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bitvector32.patch
Type: text/x-diff
Size: 2012 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080816/84f99a41/attachment-0001.bin 


More information about the Mono-devel-list mailing list