3 komentarzy

public final class UnsignedInteger extends Number implements Comparable<unsignedinteger>{

private static final long serialVersionUID = 1L;

public static long MAX_VALUE = ((long)Integer.MAX_VALUE) << 1;
public static long MIN_VALUE = 0L;

final private long value;

public UnsignedInteger(String textValue){
    this(Long.parseLong(textValue));
}

public UnsignedInteger(long longValue){
    value = longValue & 0xFFFFFFFFL;
}

public UnsignedInteger add(UnsignedInteger o){
    return new UnsignedInteger(value + o.value);
}

public UnsignedInteger add(long o){
    return new UnsignedInteger(value + o);
}

public UnsignedInteger subtract(UnsignedInteger o){
    return new UnsignedInteger(value - o.value);
}

public UnsignedInteger subtract(long o){
    return new UnsignedInteger(value - o);
}

@Override
public double doubleValue() {
    return (double)value;
}

@Override
public float floatValue() {
    return (float)value;
}

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

@Override
public long longValue() {
    return value;
}

@Override
public int compareTo(UnsignedInteger o) {
    if(value - o.value > 0)
        return 1;
    else if (value - o.value < 0)
        return -1;
    return 0;
}

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

@Override
public boolean equals(Object o){
    if (o instanceof UnsignedInteger) {
        UnsignedInteger tmp = (UnsignedInteger) o;
        return value == tmp.value;
    }
    return false;
}

@Override
public String toString(){
    return "" + value;
}
public static void main (String [] args){
    long tmp0L = 0L;
    long tmp1L = 1L;
    UnsignedInteger ui0 = new UnsignedInteger(tmp0L);
    UnsignedInteger ui_1 = ui0.subtract(tmp1L);
    System.out.println(ui_1 + " : " + Long.toBinaryString(ui_1.longValue()));
}

}

Odpowiednik w C:

#include <stdio.h>
#include <stdlib.h>

int main()
{
long tmp0L = 0L;
long tmp1L = 1L;
unsigned int ui0 = tmp0L;
unsigned int ui1 = ui0 - tmp1L;
printf("%u \n", ui1);
return 0;
}

Jak widać wynik jest taki sam czyli wartość obiektu klasy UnsignedInteger nie wychodzi poza 32 bity.

Do emulacji typów uint32 wystarczy zrzutować je na longa, a emulacja uint64 to raczej sztuka dla sztuki. Po co komu takie typy?

Zabierze się ktoś za to? :P