[futurebasic] Re: [FB] Signed and unsigned comparison

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 2007 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Sun, 13 May 2007 21:01:33 +1200
Jay Reeve wrote:

> Robert Purves wrote:
>> Comparisons (>, >=, <, <=) of pointer variables in FB are always  
>> signed. Needing an unsigned '>' comparison, I found it necessary  
>> to use the clumsy function UnsignedGreaterThan() below, with its  
>> unpleasant lapse into assembler.
>> Can someone supply a more elegant method?
>
> Here's another entry. More elegant? I don't  know. It's definitely  
> more arcane, but it's only 2 lines, pure FB, and nearly (though not  
> quite) as fast as your assembler version.

> // unsigned comparison: _zTrue if p1 > p2
> local fn UnsignedGreaterThan( p1 as pointer, p2 as pointer )
> end fn = ( p1 > p2 ) xor (( p1 xor p2 ) < 0 )

Nice method. The logical expression is easily generalised to the  
other relevant comparisons.

Signed             Unsigned
------             --------
( p1 < p2 )        ( p1 < p2 )  xor ( ( p1 xor p2 ) < 0 )
( p1 <= p2 )       ( p1 <= p2 ) xor ( ( p1 xor p2 ) < 0 )
( p1 >= p2 )       ( p1 >= p2 ) xor ( ( p1 xor p2 ) < 0 )
( p1 > p2 )        ( p1 > p2 )  xor ( ( p1 xor p2 ) < 0 )

No difference between signed and unsigned comparison:
( p1 == p2 )       ( p1 == p2 )
( p1 != p2 )       ( p1 != p2 )

Moreover, we don't need the function call, and can simply write:
   if  ( p1 > p2 ) xor ( ( p1 xor p2 ) < 0 ) then whatever...

Robert P.