Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 The Guava Authors
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
   * in compliance with the License. You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software distributed under the
  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.primitives;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.primitives.UnsignedInts.INT_MASK;
 import static com.google.common.primitives.UnsignedInts.compare;
 import static com.google.common.primitives.UnsignedInts.toLong;
 
 
 
A wrapper class for unsigned int values, supporting arithmetic operations.

In some cases, when speed is more important than code readability, it may be faster simply to treat primitive int values as unsigned, using the methods from com.google.common.primitives.UnsignedInts.

See the Guava User Guide article on unsigned primitive utilities.

Author(s):
Louis Wasserman
Since:
11.0
 
 @GwtCompatible(emulated = true)
 public final class UnsignedInteger extends Number implements Comparable<UnsignedInteger> {
   public static final UnsignedInteger ZERO = fromIntBits(0);
   public static final UnsignedInteger ONE = fromIntBits(1);
   public static final UnsignedInteger MAX_VALUE = fromIntBits(-1);
 
   private final int value;
 
   private UnsignedInteger(int value) {
     // GWT doesn't consistently overflow values to make them 32-bit, so we need to force it.
     this. = value & 0xffffffff;
   }

  
Returns an UnsignedInteger corresponding to a given bit representation. The argument is interpreted as an unsigned 32-bit value. Specifically, the sign bit of bits is interpreted as a normal bit, and all other bits are treated as usual.

If the argument is nonnegative, the returned result will be equal to bits, otherwise, the result will be equal to 2^32 + bits.

To represent unsigned decimal constants, consider valueOf(long) instead.

Since:
14.0
 
   public static UnsignedInteger fromIntBits(int bits) {
     return new UnsignedInteger(bits);
   }

  
Returns an UnsignedInteger that is equal to value, if possible. The inverse operation of longValue().
 
   public static UnsignedInteger valueOf(long value) {
     checkArgument((value & ) == value,
         "value (%s) is outside the range for an unsigned integer value"value);
     return fromIntBits((intvalue);
   }

  
Returns a UnsignedInteger representing the same value as the specified java.math.BigInteger. This is the inverse operation of bigIntegerValue().

Throws:
java.lang.IllegalArgumentException if value is negative or value >= 2^32
 
   public static UnsignedInteger valueOf(BigInteger value) {
     checkNotNull(value);
     checkArgument(value.signum() >= 0 && value.bitLength() <= .,
         "value (%s) is outside the range for an unsigned integer value"value);
     return fromIntBits(value.intValue());
   }

  
Returns an UnsignedInteger holding the value of the specified String, parsed as an unsigned int value.

Throws:
java.lang.NumberFormatException if the string does not contain a parsable unsigned int value
  public static UnsignedInteger valueOf(String string) {
    return valueOf(string, 10);
  }

  
Returns an UnsignedInteger holding the value of the specified String, parsed as an unsigned int value in the specified radix.

Throws:
java.lang.NumberFormatException if the string does not contain a parsable unsigned int value
  public static UnsignedInteger valueOf(String stringint radix) {
    return fromIntBits(UnsignedInts.parseUnsignedInt(stringradix));
  }

  
Returns the result of adding this and val. If the result would have more than 32 bits, returns the low 32 bits of the result.

Since:
14.0
    return fromIntBits(this. + checkNotNull(val).);
  }

  
Returns the result of subtracting this and val. If the result would be negative, returns the low 32 bits of the result.

Since:
14.0
    return fromIntBits( - checkNotNull(val).);
  }

  
Returns the result of dividing this by val.

Throws:
java.lang.ArithmeticException if val is zero
Since:
14.0
    return fromIntBits(UnsignedInts.divide(checkNotNull(val).));
  }

  
Returns this mod val.

Throws:
java.lang.ArithmeticException if val is zero
Since:
14.0
  public UnsignedInteger mod(UnsignedInteger val) {
    return fromIntBits(UnsignedInts.remainder(checkNotNull(val).));
  }

  
Returns the value of this UnsignedInteger as an int. This is an inverse operation to fromIntBits(int).

Note that if this UnsignedInteger holds a value >= 2^31, the returned value will be equal to this - 2^32.

  public int intValue() {
    return ;
  }

  
Returns the value of this UnsignedInteger as a long.
  public long longValue() {
    return toLong();
  }

  
Returns the value of this UnsignedInteger as a float, analogous to a widening primitive conversion from int to float, and correctly rounded.
  public float floatValue() {
    return longValue();
  }

  
Returns the value of this UnsignedInteger as a float, analogous to a widening primitive conversion from int to double, and correctly rounded.
  public double doubleValue() {
    return longValue();
  }

  
Returns the value of this UnsignedInteger as a java.math.BigInteger.
  public BigInteger bigIntegerValue() {
    return BigInteger.valueOf(longValue());
  }

  
Compares this unsigned integer to another unsigned integer. Returns 0 if they are equal, a negative number if this < other, and a positive number if this > other.
  public int compareTo(UnsignedInteger other) {
    checkNotNull(other);
    return compare(other.value);
  }
  public int hashCode() {
    return ;
  }
  public boolean equals(@Nullable Object obj) {
    if (obj instanceof UnsignedInteger) {
      UnsignedInteger other = (UnsignedIntegerobj;
      return  == other.value;
    }
    return false;
  }

  
Returns a string representation of the UnsignedInteger value, in base 10.
  public String toString() {
    return toString(10);
  }

  
Returns a string representation of the UnsignedInteger value, in base radix. If radix < Character.MIN_RADIX or radix > Character.MAX_RADIX, the radix 10 is used.
  public String toString(int radix) {
    return UnsignedInts.toString(radix);
  }
New to GrepCode? Check out our FAQ X