BitString (long from, long to) { if (from == to) { return new BitString(from, -1L); } long bits = 0; long mask = -1; while (true) { int fromBit = Long.SIZE - 1 - Long.numberOfLeadingZeros(from); int toBit = Long.SIZE - 1 - Long.numberOfLeadingZeros(to); if (fromBit != toBit) { for (int i = 0; i <= Math.max(fromBit, toBit); i++) { mask = clearBit(mask, i); } break; } if (fromBit == 0) break; bits = setBit(bits, fromBit); from = clearBit(from, fromBit); to = clearBit(to, fromBit); } return new BitString(bits, mask); }