Numeric comparisons

Doug Pardee doug at terak.UUCP
Sat Aug 31 04:29:16 AEST 1985


> ... since comparison is merely done by subtraction (a
> compare instruction is usually just a subtract instruction that doesn't
> store the result anywhere)...

Compiler writers note -- a comparison is not "just a subtract".
For example:

#define NBITS 16
/* NBITS is number of bits in an integer */
   int a, b;
   a = 3 << (NBITS-3);  /* 24576 for NBITS=16 */
   b = -a;
   if (a>b)
      printf("Comparison was done by bit-wise comparison\n");
   else
      printf("Comparison was done by subtraction\n");   /* WRONG */

Compiler users note -- if your compiler gives the wrong results, the
compiler writer might not be completely at fault.  Many early CPU
chips (8080A, Z80, 6502, etc.) did comparison by subtraction, and a
compiler would have had to generate extra code to test for Overflow
in order to get the correct result.
-- 
Doug Pardee -- CalComp -- {seismo!noao,decvax!noao,ihnp4}!terak!doug



More information about the Comp.lang.c mailing list