For instance, whenever my count is equal to 4294967295 1 I want that the count restart from 0. Basically I`m trying to simulate a unsigned 32 bits in java that has range [0, 4294967295]. The methods that I'm trying to create is ADD and MULTIPLE.
For example, if I have the number 4294967295 and I multiple by 2, I will get the result 4294967294.
Since Java does not have a unsigned int, I need to create that class to "simulate" the C int uint32_t in Java.
If I multiple 1103527590 * 1103515245 the result should be 4294967294, but my code is giving the result 2524872877
public class Unsigned32BitsInt {
private long limit; // Upper bound 4294967295
public Unsigned32BitsInt(long limit) {
this.limit = limit;
}
public long add(long x, long y) {
long result = 0L;
if (x y < this.limit) {
if ((x y) % this.limit == 0) {
result = (x y) / 2;
} else {
result = (x y) % this.limit;
}
} else {
if ((x y) % this.limit == 0) {
result = (x y) / (Math.max(x, y) / Math.min(x, y)) - 1;
} else {
result = (x y) % this.limit - 1;
}
}
return result;
}
public long multiple(long x, long y) {
long result = 1L;
long r = x*y;
long c = result = (x * y) % this.limit;
if (x * y < this.limit) {
if ((x * y) % this.limit == 0) {
result = (x y) / 2;
} else {
result = (x * y) % this.limit;
}
} else {
if ((x * y) % this.limit == 0) {
result = (x * y) / 2 - 1;
} else {
result = (x * y) % this.limit - 1;
}
}
return result;
}
}
CodePudding user response:
Int's are only signed from the standpoint of comparisons and display. Internally signed and unsigned are no different.
So when the count is 4294967295
that is -1
in twos complement form. So you add 1
and you get 0
as required. You can print it unsigned by casting it to a long like so.
int s = -1;
// prints 4294967295
System.out.println(((long)s)&0xffff_ffffL);
If count is Integer.MAX_VALUE
you have count = 2147483647
you can add 1
and get 2147483648
. You can cast it to a long to print it as unsigned instead of the normal -2147483648
.
To do general comparisons, use the built-in method
Integer.unsignedCompare(int x, int y);
to compare two integers as 32
bit unsigned values.
And finally,
int a = 1_103_527_590;
int b = 1_103_515_245;
System.out.println((long)(a*b) & 0xffff_ffffL);
prints
2524872878
CodePudding user response:
This class simulates some of a 32-bit Unsigned Integer in Java. I kept the variable as a 32-bit int
public class Unsigned32BitInt {
private int value;
public Unsigned32BitInt () { value = 0; }
public Unsigned32BitInt (long v) {
value = (int) v ;
}
public Unsigned32BitInt (int v) {
value = v;
}
public Unsigned32BitInt add (Unsigned32BitInt i) {
long a = i.value;
long b = value;
long sum = a b;
return new Unsigned32BitInt (sum);
}
public Unsigned32BitInt multiply (Unsigned32BitInt i) {
long a = i.value;
long b = value;
return new Unsigned32BitInt (a * b);
}
@Override
public String toString () {
return Long.toString (0x00000000FFFFFFFFL & value);
}
You can add your own methods and overloaded methods.
You might want to throw an exception if there is an attempt to pass a negative number as an argument. However, since the code shown calls a constructor in the add
and the multiply
methods, you would have to be careful about throwing such an exception from a constructor.
The leading zeros in the mask 0x00000000FFFFFFFFL
are not necessary .