-2

I just learned about the magnitude comparator circuit. A MC would tell with three bits:

m < n
m = n
m > n

So far I did not encouter a programming language, that would offer such an operation. I also can't remember an assembly statement for that. Is there any specific reason to not provide such a functionality to a programming language, if the circuit is present anyway?

TheTrowser
  • 363
  • 4
  • 14
  • If there's no assembly instruction for it, there's no way for software to use it. Do common CPUs even include these devices? – Wyzard May 20 '17 at 05:21
  • 2
    In a CPU this information is traditionally produced as a side-effect of the basic addition/subtraction operation within an ALU. Frequently through a comparison instruction which is effectively a subtraction evaluated for the status bits rather than storing the results. Usually a flag status register encodes the arithmetic relationship as combination of zero (equality) and carry/borrow flag bits (and perhaps a signed overflow flag), though some architectures do not explicitly store flags and only evaluate one condition at a time. – doynax May 20 '17 at 05:30
  • I was about to mention that too — for example, on x86, after a subtraction or comparison instruction, the zero flag is set if the numbers were equal, and the carry flag is set if the second number was larger. If both flags are clear, the first number was larger. Internally, those flags might be set by the output of a magnitude comparator. – Wyzard May 20 '17 at 05:33

1 Answers1

1

There are similar examples in programming languages, e.g. strcmp() and strcoll() and memcmp() in C return a number less than, equal to, or greater than zero, depending on the order their arguments sort in.

Similarly in Perl, the <=> and cmp operators return -1, 0, or 1, again depending on which of the operands is larger (numerically for <=>, stringwise comparison for cmp).

Of course none of those produces three separate bits. (With only three options, two bits would be enough anyway.) A numerical return value makes it easier to do a "greater or equal" test by just testing the output with >= 0. Most programs process mostly numbers, and the languages and processors make it easy, so a numerical return is not ineffective, and an output of individual bits might seem out-of-place.

(Of course we could define such a comparison function with the return values 4, 2 and 1, and then ask the programmer to use symbolic constants or special test functions to turn those into human-understandable terms.)

ilkkachu
  • 6,221
  • 16
  • 30