public static bool operator != (Section v1, Section v2)
{
- return v1.mask != v2.mask &&
+ return v1.mask != v2.mask ||
v1.offset != v2.offset;
}
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)
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");
}
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 ()
}
// 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;
- }
}
}