Class UnsignedInts


  • @Beta
    @GwtCompatible
    public final class UnsignedInts
    extends Object
    Static utility methods pertaining to int primitives that interpret values as unsigned (that is, any negative value x is treated as the positive value 2^32 + x). The methods for which signedness is not an issue are in Ints, as well as signed versions of methods for which signedness is an issue.

    In addition, this class provides several static methods for converting an int to a String and a String to an int that treat the int as an unsigned number.

    Users of these utilities must be extremely careful not to mix up signed and unsigned int values. When possible, it is recommended that the UnsignedInteger wrapper class be used, at a small efficiency penalty, to enforce the distinction in the type system.

    See the Guava User Guide article on unsigned primitive utilities.

    Since:
    11.0
    Author:
    Louis Wasserman
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int checkedCast​(long value)
      Returns the int value that, when treated as unsigned, is equal to value, if possible.
      static int compare​(int a, int b)
      Compares the two specified int values, treating them as unsigned values between 0 and 2^32 - 1 inclusive.
      static int decode​(String stringValue)
      Returns the unsigned int value represented by the given string.
      static int divide​(int dividend, int divisor)
      Returns dividend / divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.
      static String join​(String separator, int... array)
      Returns a string containing the supplied unsigned int values separated by separator.
      static Comparator<int[]> lexicographicalComparator()
      Returns a comparator that compares two arrays of unsigned int values lexicographically.
      static int max​(int... array)
      Returns the greatest value present in array, treating values as unsigned.
      static int min​(int... array)
      Returns the least value present in array, treating values as unsigned.
      static int parseUnsignedInt​(String s)
      Returns the unsigned int value represented by the given decimal string.
      static int parseUnsignedInt​(String string, int radix)
      Returns the unsigned int value represented by a string with the given radix.
      static int remainder​(int dividend, int divisor)
      Returns dividend % divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.
      static int saturatedCast​(long value)
      Returns the int value that, when treated as unsigned, is nearest in value to value.
      static void sort​(int[] array)
      Sorts the array, treating its elements as unsigned 32-bit integers.
      static void sort​(int[] array, int fromIndex, int toIndex)
      Sorts the array between fromIndex inclusive and toIndex exclusive, treating its elements as unsigned 32-bit integers.
      static void sortDescending​(int[] array)
      Sorts the elements of array in descending order, interpreting them as unsigned 32-bit integers.
      static void sortDescending​(int[] array, int fromIndex, int toIndex)
      Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order, interpreting them as unsigned 32-bit integers.
      static long toLong​(int value)
      Returns the value of the given int as a long, when treated as unsigned.
      static String toString​(int x)
      Returns a string representation of x, where x is treated as unsigned.
      static String toString​(int x, int radix)
      Returns a string representation of x for the given radix, where x is treated as unsigned.
    • Method Detail

      • compare

        public static int compare​(int a,
                                  int b)
        Compares the two specified int values, treating them as unsigned values between 0 and 2^32 - 1 inclusive.

        Java 8 users: use Integer.compareUnsigned(int, int) instead.

        Parameters:
        a - the first unsigned int to compare
        b - the second unsigned int to compare
        Returns:
        a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal
      • toLong

        public static long toLong​(int value)
        Returns the value of the given int as a long, when treated as unsigned.

        Java 8 users: use Integer.toUnsignedLong(int) instead.

      • checkedCast

        public static int checkedCast​(long value)
        Returns the int value that, when treated as unsigned, is equal to value, if possible.
        Parameters:
        value - a value between 0 and 232-1 inclusive
        Returns:
        the int value that, when treated as unsigned, equals value
        Throws:
        IllegalArgumentException - if value is negative or greater than or equal to 232
        Since:
        21.0
      • saturatedCast

        public static int saturatedCast​(long value)
        Returns the int value that, when treated as unsigned, is nearest in value to value.
        Parameters:
        value - any long value
        Returns:
        2^32 - 1 if value >= 2^32, 0 if value <= 0, and value cast to int otherwise
        Since:
        21.0
      • min

        public static int min​(int... array)
        Returns the least value present in array, treating values as unsigned.
        Parameters:
        array - a nonempty array of unsigned int values
        Returns:
        the value present in array that is less than or equal to every other value in the array according to compare(int, int)
        Throws:
        IllegalArgumentException - if array is empty
      • max

        public static int max​(int... array)
        Returns the greatest value present in array, treating values as unsigned.
        Parameters:
        array - a nonempty array of unsigned int values
        Returns:
        the value present in array that is greater than or equal to every other value in the array according to compare(int, int)
        Throws:
        IllegalArgumentException - if array is empty
      • join

        public static String join​(String separator,
                                  int... array)
        Returns a string containing the supplied unsigned int values separated by separator. For example, join("-", 1, 2, 3) returns the string "1-2-3".
        Parameters:
        separator - the text that should appear between consecutive values in the resulting string (but not at the start or end)
        array - an array of unsigned int values, possibly empty
      • sort

        public static void sort​(int[] array)
        Sorts the array, treating its elements as unsigned 32-bit integers.
        Since:
        23.1
      • sort

        public static void sort​(int[] array,
                                int fromIndex,
                                int toIndex)
        Sorts the array between fromIndex inclusive and toIndex exclusive, treating its elements as unsigned 32-bit integers.
        Since:
        23.1
      • sortDescending

        public static void sortDescending​(int[] array)
        Sorts the elements of array in descending order, interpreting them as unsigned 32-bit integers.
        Since:
        23.1
      • sortDescending

        public static void sortDescending​(int[] array,
                                          int fromIndex,
                                          int toIndex)
        Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order, interpreting them as unsigned 32-bit integers.
        Since:
        23.1
      • divide

        public static int divide​(int dividend,
                                 int divisor)
        Returns dividend / divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.

        Java 8 users: use Integer.divideUnsigned(int, int) instead.

        Parameters:
        dividend - the dividend (numerator)
        divisor - the divisor (denominator)
        Throws:
        ArithmeticException - if divisor is 0
      • remainder

        public static int remainder​(int dividend,
                                    int divisor)
        Returns dividend % divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.

        Java 8 users: use Integer.remainderUnsigned(int, int) instead.

        Parameters:
        dividend - the dividend (numerator)
        divisor - the divisor (denominator)
        Throws:
        ArithmeticException - if divisor is 0
      • decode

        @CanIgnoreReturnValue
        public static int decode​(String stringValue)
        Returns the unsigned int value represented by the given string.

        Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix:

        • 0xHexDigits
        • 0XHexDigits
        • #HexDigits
        • 0OctalDigits
        Throws:
        NumberFormatException - if the string does not contain a valid unsigned int value
        Since:
        13.0