[Mono-dev] BitVector32 patch

Miguel de Icaza miguel at novell.com
Wed Aug 20 16:09:45 EDT 2008


Hello,

    Scott, would you mind providing NUnit test cases to ensure that this
bug does not ever creep up again, and in particular, test cases for the
current failure?

> 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;
> -        }
>      }
>  }
> 
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list



More information about the Mono-devel-list mailing list