2009-08-20 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System / System.Collections.Specialized / BitVector32.cs
index 8669cf8b1d90dc8c7df7498d977f540db7d13a87..3002ca8ab4078d6a4698b1c24ebbf8347dfbbf12 100644 (file)
@@ -62,7 +62,7 @@ namespace System.Collections.Specialized {
 
                        public static bool operator != (Section v1, Section v2)
                        {
-                               return v1.mask != v2.mask &&
+                               return v1.mask != v2.mask ||
                                       v1.offset != v2.offset;
                        }
 
@@ -84,14 +84,12 @@ namespace System.Collections.Specialized {
 
                        public override int GetHashCode ()
                        {
-                               return (((Int16) mask).GetHashCode () << 16) + 
-                                      ((Int16) offset).GetHashCode ();
+                               return mask << offset; 
                        }
                        
                        public override string ToString ()
                        {
-                               return "Section{0x" + Convert.ToString(mask, 16) + 
-                                      ", 0x" + Convert.ToString(offset, 16) + "}";
+                               return ToString (this); 
                        }
 
                        public static string ToString (Section value)
@@ -184,11 +182,11 @@ namespace System.Collections.Specialized {
                        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");
                        }
 
@@ -197,10 +195,7 @@ namespace System.Collections.Specialized {
                
                public override bool Equals (object o)
                {
-                       if (!(o is BitVector32))
-                               return false;
-
-                       return bits == ((BitVector32) o).bits;
+                       return (o is BitVector32) && bits == ((BitVector32) o).bits;
                }
 
                public override int GetHashCode ()
@@ -227,27 +222,12 @@ namespace System.Collections.Specialized {
                }
 
                // 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;
-               }
        }
 }