Emulacja liczb unsigned


Strona w budowie
Ktoś pracuje nad tą stroną, jej zawartość może się wkrótce zmienić. Prosimy o cierpliwość!



Język Java nie udostępnia typów unsigned. W tym artykule opisane są metody "emulacji" tego typu liczb w Javie.
Informacje
Ostatnia modyfikacja 12-02-2008 19:16 Ostatni autor Koziołek
Ilość wyświetleń 1764 Wersja 2
Komentarz
turok dnia 20-05-2010 12:20
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.
donkey7 dnia 24-01-2010 17:20
Do emulacji typów uint32 wystarczy zrzutować je na longa, a emulacja uint64 to raczej sztuka dla sztuki. Po co komu takie typy?
Keraj dnia 10-05-2009 21:05
Zabierze się ktoś za to? :P

Copyright © 2000-2006 by Coyote Group 0.9.3-pre3
Czas generowania strony: 0.0217 sek. (zapytań SQL: 11)