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;
 
 
 
A wrapper class for unsigned long values, supporting arithmetic operations.

In some cases, when speed is more important than code readability, it may be faster simply to treat primitive long values as unsigned, using the methods from UnsignedLongs.

See the Guava User Guide article on unsigned primitive utilities.

Author(s):
Louis Wasserman
Colin Evans
Since:
11.0
 
 @GwtCompatible(serializable = true)
 public final class UnsignedLong extends Number implements Comparable<UnsignedLong>, Serializable {
 
   private static final long UNSIGNED_MASK = 0x7fffffffffffffffL;
 
   public static final UnsignedLong ZERO = new UnsignedLong(0);
   public static final UnsignedLong ONE = new UnsignedLong(1);
   public static final UnsignedLong MAX_VALUE = new UnsignedLong(-1L);
 
   private final long value;
 
   private UnsignedLong(long value) {
     this. = value;
   }

  
Returns an UnsignedLong corresponding to a given bit representation. The argument is interpreted as an unsigned 64-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^64 + bits.

To represent decimal constants less than 2^63, consider valueOf(long) instead.

Since:
14.0
 
   public static UnsignedLong fromLongBits(long bits) {
     // TODO(lowasser): consider caching small values, like Long.valueOf
     return new UnsignedLong(bits);
   }

  
Returns an UnsignedLong representing the same value as the specified long.

Throws:
java.lang.IllegalArgumentException if value is negative
Since:
14.0
 
   public static UnsignedLong valueOf(long value) {
     checkArgument(value >= 0, "value (%s) is outside the range for an unsigned long value"value);
     return fromLongBits(value);
   }

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

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

  
Returns an UnsignedLong holding the value of the specified String, parsed as an unsigned long value.

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

  
Returns an UnsignedLong holding the value of the specified String, parsed as an unsigned long value in the specified radix.

Throws:
java.lang.NumberFormatException if the string does not contain a parsable unsigned long value, or radix is not between java.lang.Character.MIN_RADIX and java.lang.Character.MAX_RADIX
  public static UnsignedLong valueOf(String stringint radix) {
    return fromLongBits(UnsignedLongs.parseUnsignedLong(stringradix));
  }

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

Since:
14.0
  public UnsignedLong plus(UnsignedLong val) {
    return fromLongBits(this. + checkNotNull(val).);
  }

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

Since:
14.0
  public UnsignedLong minus(UnsignedLong val) {
    return fromLongBits(this. - checkNotNull(val).);
  }

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

Since:
14.0
  public UnsignedLong times(UnsignedLong val) {
    return fromLongBits( * checkNotNull(val).);
  }

  
Returns the result of dividing this by val.

Since:
14.0
  public UnsignedLong dividedBy(UnsignedLong val) {
    return fromLongBits(UnsignedLongs.divide(checkNotNull(val).));
  }

  
Returns this modulo val.

Since:
14.0
  public UnsignedLong mod(UnsignedLong val) {
    return fromLongBits(UnsignedLongs.remainder(checkNotNull(val).));
  }

  
Returns the value of this UnsignedLong as an int.
  public int intValue() {
    return (int;
  }

  
Returns the value of this UnsignedLong as a long. This is an inverse operation to fromLongBits(long).

Note that if this UnsignedLong holds a value >= 2^63, the returned value will be equal to this - 2^64.

  public long longValue() {
    return ;
  }

  
Returns the value of this UnsignedLong as a float, analogous to a widening primitive conversion from long to float, and correctly rounded.
  public float floatValue() {
    @SuppressWarnings("cast")
    float fValue = (float) ( & );
    if ( < 0) {
      fValue += 0x1.0p63f;
    }
    return fValue;
  }

  
Returns the value of this UnsignedLong as a double, analogous to a widening primitive conversion from long to double, and correctly rounded.
  public double doubleValue() {
    @SuppressWarnings("cast")
    double dValue = (double) ( & );
    if ( < 0) {
      dValue += 0x1.0p63;
    }
    return dValue;
  }

  
Returns the value of this UnsignedLong as a java.math.BigInteger.
  public BigInteger bigIntegerValue() {
    BigInteger bigInt = BigInteger.valueOf( & );
    if ( < 0) {
      bigInt = bigInt.setBit(. - 1);
    }
    return bigInt;
  }
  public int compareTo(UnsignedLong o) {
    checkNotNull(o);
    return UnsignedLongs.compare(o.value);
  }
  public int hashCode() {
    return Longs.hashCode();
  }
  public boolean equals(@Nullable Object obj) {
    if (obj instanceof UnsignedLong) {
      UnsignedLong other = (UnsignedLongobj;
      return  == other.value;
    }
    return false;
  }

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

  
Returns a string representation of the UnsignedLong 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 UnsignedLongs.toString(radix);
  }
New to GrepCode? Check out our FAQ X