/** * Subtraction. Will never underflow, but wraps around when the lowest * ip address has been reached. * * @param value value to subtract * @return new IPv6 address */ public IPv6Address subtract(int value) { final long newLowBits = lowBits - value; if (value >= 0) { if (isLessThanUnsigned(lowBits, newLowBits)) { // oops, we subtracted something positive and the result is bigger -> overflow detected (carry over one bit from high to low) return new IPv6Address(highBits - 1, newLowBits); } else { // no overflow return new IPv6Address(highBits, newLowBits); } } else { if (isLessThanUnsigned(newLowBits, lowBits)) { // oops, we subtracted something negative and the result is smaller -> overflow detected (carry over one bit from low to high) return new IPv6Address(highBits + 1, newLowBits); } else { // no overflow return new IPv6Address(highBits, newLowBits); } } }
/** * Addition. Will never overflow, but wraps around when the highest ip * address has been reached. * * @param value value to add * @return new IPv6 address */ public IPv6Address add(int value) { final long newLowBits = lowBits + value; if (value >= 0) { if (isLessThanUnsigned(newLowBits, lowBits)) { // oops, we added something positive and the result is smaller -> overflow detected (carry over one bit from low to high) return new IPv6Address(highBits + 1, newLowBits); } else { // no overflow return new IPv6Address(highBits, newLowBits); } } else { if (isLessThanUnsigned(lowBits, newLowBits)) { // oops, we added something negative and the result is bigger -> overflow detected (carry over one bit from high to low) return new IPv6Address(highBits - 1, newLowBits); } else { // no overflow return new IPv6Address(highBits, newLowBits); } } }
/** * Addition. Will never overflow, but wraps around when the highest ip * address has been reached. * * @param value value to add * @return new IPv6 address */ public IPv6Address add(int value) { final long newLowBits = lowBits + value; if (value >= 0) { if (isLessThanUnsigned(newLowBits, lowBits)) { // oops, we added something positive and the result is smaller -> overflow detected (carry over one bit from low to high) return new IPv6Address(highBits + 1, newLowBits); } else { // no overflow return new IPv6Address(highBits, newLowBits); } } else { if (isLessThanUnsigned(lowBits, newLowBits)) { // oops, we added something negative and the result is bigger -> overflow detected (carry over one bit from high to low) return new IPv6Address(highBits - 1, newLowBits); } else { // no overflow return new IPv6Address(highBits, newLowBits); } } }
/** * Subtraction. Will never underflow, but wraps around when the lowest * ip address has been reached. * * @param value value to subtract * @return new IPv6 address */ public IPv6Address subtract(int value) { final long newLowBits = lowBits - value; if (value >= 0) { if (isLessThanUnsigned(lowBits, newLowBits)) { // oops, we subtracted something positive and the result is bigger -> overflow detected (carry over one bit from high to low) return new IPv6Address(highBits - 1, newLowBits); } else { // no overflow return new IPv6Address(highBits, newLowBits); } } else { if (isLessThanUnsigned(newLowBits, lowBits)) { // oops, we subtracted something negative and the result is smaller -> overflow detected (carry over one bit from low to high) return new IPv6Address(highBits + 1, newLowBits); } else { // no overflow return new IPv6Address(highBits, newLowBits); } } }