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?
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.)