This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Language extensions ?


Tom Tromey writes:
 > >>>>> "Robin" == Robin Garner <robin dot garner at iname dot 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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]