Class to represent unsigned 32-bit numbers. : Binary Bit « Language Basics « Java






Class to represent unsigned 32-bit numbers.

     
/*
 * D-Bus Java Implementation Copyright (c) 2005-2006 Matthew Johnson This
 * program is free software; you can redistribute it and/or modify it under the
 * terms of either the GNU Lesser General Public License Version 2 or the
 * Academic Free Licence Version 2.1. Full licence texts are included in the
 * COPYING file with this program.
 */
//package org.freedesktop.dbus;

import java.text.MessageFormat;

/**
 * Class to represent unsigned 32-bit numbers.
 */
@SuppressWarnings("serial")
public class UInt32 extends Number implements Comparable<UInt32> {
  /** Maximum allowed value */
  public static final long MAX_VALUE = 4294967295L;
  /** Minimum allowed value */
  public static final long MIN_VALUE = 0;
  private long             value;

  /**
   * Create a UInt32 from a long.
   * 
   * @param value
   *          Must be a valid integer within MIN_VALUE&ndash;MAX_VALUE
   * @throws NumberFormatException
   *           if value is not between MIN_VALUE and MAX_VALUE
   */
  public UInt32(long value) {
    this.value = value;
  }

  /**
   * Create a UInt32 from a String.
   * 
   * @param value
   *          Must parse to a valid integer within MIN_VALUE&ndash;MAX_VALUE
   * @throws NumberFormatException
   *           if value is not an integer between MIN_VALUE and MAX_VALUE
   */
  public UInt32(String value) {
    this(Long.parseLong(value));
  }

  /** The value of this as a byte. */
  @Override
  public byte byteValue() {
    return (byte) value;
  }

  /**
   * Compare two UInt32s.
   * 
   * @return 0 if equal, -ve or +ve if they are different.
   */
  public int compareTo(UInt32 other) {
    return (int) (value - other.value);
  }

  /** The value of this as a double. */
  @Override
  public double doubleValue() {
    return value;
  }

  /** Test two UInt32s for equality. */
  @Override
  public boolean equals(Object o) {
    return (o instanceof UInt32) && (((UInt32) o).value == value);
  }

  /** The value of this as a float. */
  @Override
  public float floatValue() {
    return value;
  }

  @Override
  public int hashCode() {
    return (int) value;
  }

  /** The value of this as a int. */
  @Override
  public int intValue() {
    return (int) value;
  }

  /** The value of this as a long. */
  @Override
  public long longValue() {
    return /* (long) */value;
  }

  /** The value of this as a short. */
  @Override
  public short shortValue() {
    return (short) value;
  }

  /** The value of this as a string */
  @Override
  public String toString() {
    return "" + value;
  }
}

   
    
    
    
    
  








Related examples in the same category

1. Utility for byte swapping of all java data types
2.Bitwise DemoBitwise Demo
3.Binary Digits
4.Using the bitwise operatorsUsing the bitwise operators
5.Bitwise complement (~): inverts ones and zeroes in a number
6.Convert a number to negative and back
7.Bitwise AND (&)
8.Bitwise OR (|)
9.Bitwise XOR (^)
10.Left shift (<<)
11.Performing Bitwise Operations on a Bit Vector
12.Converting Between a BitSet and a Byte Array
13.Returns a byte array of at least length 1
14.Shift to the left
15.Signed shift to the right
16.Unsigned shift to the right
17.Bit-level unpacking of floating-point data
18.Gets the a single bit of the target.
19.Returns 16 bits from the long number.
20.Sets a specific bit of an int.
21.Fuses the lower 16 bits of two ints.
22.A list of bits.
23.Gets the specified bit (0-31) from the integer argument.
24.Sets the specified bit (0-31) in the integer argument.
25.Clears a range of bits in the specified integer.