Java Data Type Tutorial - Java Unsigned Data Type








Java does not support unsigned data types.

The byte, short, int, and long are all signed data types.

For a signed data type, half of the range of values stores positive number and half for negative numbers, as one bit is used to store the sign of the value.

For example, a byte takes 8 bits; its range is -128 to 127. If you were to store only positive numbers in a byte, its range would have been 0 to 255.

Java has some static methods in wrapper classes to support operations treating the bits in the signed values as if they are unsigned integers.

The Byte class contains two static methods:

int  toUnsignedInt(byte x)
long  toUnsignedLong(byte x)

The methods convert the specified byte argument into an int and a long as if the byte stores an unsigned value.

If the specified byte is zero or positive, the converted int and long values will be the same.

If the argument is a negative number, the converted number will be 28 + x.

For example, for an input of -10, the returned value will be 28 + (-10), which is 246.

Negative numbers are stored in 2's complement form. The value -10 will be stored as 11110110. The most significant bit 1 indicates that it is a negative number.

The 2's complement of the first 7 bits (1110110) would be 001010, which is 10 in decimal.

If you consider the actual bits, 11110110, in a byte as an unsigned integer, its value is 246 (128 + 64 + 32 + 16 + 0 + 4 + 2 + 0).





Example

The following code shows how to get the value stored in a byte as an unsigned integer:

public class Main {
  public static void main(String[] args) {
    byte b = -10;
    int x = Byte.toUnsignedInt(b);
    System.out.println("Signed value in byte   = " + b);
    System.out.println("Unsigned value in  byte   = " + x);

  }
}

The code above generates the following result.





Example 2

The Integer class contains the following static methods to support unsigned operations and conversions:

int compareUnsigned(int x, int y)
int  divideUnsigned(int dividend, int divisor)
int  parseUnsignedInt(String s)
int  parseUnsignedInt(String s, int radix)
int  remainderUnsigned(int dividend,  int divisor)
long  toUnsignedLong(int x)
String toUnsignedString(int i)
String toUnsignedString(int i, int radix)

The following code shows the division operation on two int variables as if their bits represent unsigned values:

public class Main {
  public static void main(String[] args) {
    // Two negative integer values
    int x = -1;
    int y = -2;//w w  w.  j  a  v a  2 s  .com

    // Performs signed division
    System.out.println("Signed x = " + x);
    System.out.println("Signed y = " + y);
    System.out.println("Signed x/y  = " + (x / y));

    // Performs unsigned division by treating x and y holding unsigned values
    long ux = Integer.toUnsignedLong(x);
    long uy = Integer.toUnsignedLong(y);
    int uQuotient = Integer.divideUnsigned(x, y);
    System.out.println("Unsigned x  = " + ux);
    System.out.println("Unsigned y  = " + uy);
    System.out.println("Unsigned x/y  = " + uQuotient);
  }
}

The code above generates the following result.