Java Unsigned Number Create unsignedLongToString(long x)

Here you can find the source of unsignedLongToString(long x)

Description

Returns a string representation of x, where x is treated as unsigned.

License

Apache License

Declaration

public static String unsignedLongToString(long x) 

Method Source Code

//package com.java2s;
//Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**/* w ww  . j  a va 2  s.c o  m*/
     * Returns a string representation of x, where x is treated as unsigned.
     */
    public static String unsignedLongToString(long x) {
        return unsignedLongToString(x, 10);
    }

    /**
     * Returns a string representation of {@code x} for the given radix, where {@code x} is treated as unsigned.
     *
     * @param x
     *            the value to convert to a string.
     * @param radix
     *            the radix to use while working with {@code x}
     * @throws IllegalArgumentException
     *             if {@code radix} is not between {@link Character#MIN_RADIX} and {@link Character#MAX_RADIX}.
     */
    private static String unsignedLongToString(long x, int radix) {
        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
            throw new IllegalArgumentException("Invalid radix: " + radix);
        }
        if (x == 0) {
            // Simply return "0"
            return "0";
        } else {
            char[] buf = new char[64];
            int i = buf.length;
            if (x < 0) {
                // Separate off the last digit using unsigned division. That will leave
                // a number that is nonnegative as a signed integer.
                long quotient = divide(x, radix);
                long rem = x - quotient * radix;
                buf[--i] = Character.forDigit((int) rem, radix);
                x = quotient;
            }
            // Simple modulo/division approach
            while (x > 0) {
                buf[--i] = Character.forDigit((int) (x % radix), radix);
                x /= radix;
            }
            // Generate string
            return new String(buf, i, buf.length - i);
        }
    }

    /**
     * Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.
     *
     * @param dividend
     *            the dividend (numerator)
     * @param divisor
     *            the divisor (denominator)
     * @throws ArithmeticException
     *             if divisor is 0
     */
    private static long divide(long dividend, long divisor) {
        if (divisor < 0) { // i.e., divisor >= 2^63:
            if (compareUnsigned(dividend, divisor) < 0) {
                return 0; // dividend < divisor
            } else {
                return 1; // dividend >= divisor
            }
        }

        // Optimization - use signed division if dividend < 2^63
        if (dividend >= 0) {
            return dividend / divisor;
        }

        /*
         * Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is guaranteed to be
         * either exact or one less than the correct value. This follows from fact that floor(floor(x)/i) == floor(x/i)
         * for any real x and integer i != 0. The proof is not quite trivial.
         */
        long quotient = ((dividend >>> 1) / divisor) << 1;
        long rem = dividend - quotient * divisor;
        return quotient + (compareUnsigned(rem, divisor) >= 0 ? 1 : 0);
    }

    /**
     * Compares the two specified {@code long} values, treating them as unsigned values between {@code 0} and
     * {@code 2^64 - 1} inclusive.
     *
     * @param a
     *            the first unsigned {@code long} to compare
     * @param b
     *            the second unsigned {@code long} to compare
     * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is greater than
     *         {@code b}; or zero if they are equal
     */
    private static int compareUnsigned(long a, long b) {
        return compareSigned(flip(a), flip(b));
    }

    /**
     * Compares the two specified {@code long} values. The sign of the value returned is the same as that of
     * {@code ((Long) a).compareTo(b)}.
     *
     * <p>
     * <b>Note for Java 7 and later:</b> this method should be treated as deprecated; use the equivalent
     * {@link Long#compare} method instead.
     *
     * @param a
     *            the first {@code long} to compare
     * @param b
     *            the second {@code long} to compare
     * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is greater than
     *         {@code b}; or zero if they are equal
     */
    private static int compareSigned(long a, long b) {
        return (a < b) ? -1 : ((a > b) ? 1 : 0);
    }

    private static int flip(int value) {
        return value ^ Integer.MIN_VALUE;
    }

    /**
     * A (self-inverse) bijection which converts the ordering on unsigned longs to the ordering on longs, that is,
     * {@code a <= b} as unsigned longs if and only if {@code flip(a) <= flip(b)} as signed longs.
     */
    private static long flip(long a) {
        return a ^ Long.MIN_VALUE;
    }
}

Related

  1. unsignedLocalIntersect2by2(final short[] set1, final int length1, final short[] set2, final int length2, final short[] buffer)
  2. unsignedLocalIntersect2by2Cardinality(final short[] set1, final int length1, final short[] set2, final int length2)
  3. unsignedLong(byte b)
  4. unsignedLongToByteArray(final long value)
  5. unsignedLongToString(long value)
  6. unsignedMediumToBytes(final Long unsignedInt)
  7. unsignedNumericToByteArray(long src, int length)
  8. unsignedOneSidedGallopingIntersect2by2(final short[] smallSet, final int smallLength, final short[] largeSet, final int largeLength, final short[] buffer)
  9. unsignedShort(byte b)