Numeric comparisons
Ken Turkowski
ken at turtlevax.UUCP
Wed Sep 25 11:27:50 AEST 1985
In article <726 at terak.UUCP> doug at terak.UUCP (Doug Pardee) writes:
> ...
>Fortunately, the more modern CPU chips are designed with a proper
>compare instruction which does bit-wise comparison instead of
>subtraction.
What a bunch of BS! A compare is simply a subtract with the result
thrown away. You imply that a compare does an exclusive-OR, which will
compare only for equality, but not for ordering.
The conditions used for the branch will determine how the operands are
to be compared: whether as signed integers or unsigned integers. A
subtraction sets 4 bits: negative (N), zero (Z), carry (C), and
overflow (V). They are combined to indicate ordering relationships as
follows:
signed < N ^ V
signed <= (N ^ V) | Z
signed > !(N ^ V) & !Z
signed >= !(N ^ V)
unsigned < !C
unsigned <= !C | Z
unsigned > C & !Z
unsigned >= C
== Z
!= !Z
Note that some machines generate a borrow (B) rather than a carry after
subtraction. Just substitute a !B for C above.
--
Ken Turkowski @ CADLINC, Menlo Park, CA
UUCP: {amd,decwrl,hplabs,seismo,spar}!turtlevax!ken
ARPA: turtlevax!ken at DECWRL.ARPA
More information about the Comp.lang.c
mailing list