Language extensions ?
Andrew Haley
aph@redhat.com
Fri Mar 28 10:00:00 GMT 2003
Tom Tromey writes:
> >>>>> "Robin" == Robin Garner <robin.garner@iname.com> writes:
>
> Robin> Sadly, the only way to compare signed integers in an unsigned
> Robin> way is:
> Robin> boolean GT(int a, int b) {
> Robin> if( a > 0 && b > 0 || a < 0 && b < 0 )
> Robin> return a > b;
> Robin> else
> Robin> return a < b;
> Robin> }
>
> Sounds like a puzzle :-)
>
> How about
>
> boolean GT(int a, int b) {
> return (((long) a) & 0xffffffffL) > (((long) b) & 0xffffffffL);
> }
It's two's complement arithmetic. Just remove the bias and do the
comparison.
(a ^ 0x80000000) > (b ^ 0x80000000)
Andrew.
More information about the Java
mailing list