You only need implementations of
<
and==
as the rest can all be derived:
op shorthand for a!=b
not(a==b)
a<=b
not(b<a)
a>b
b<a
a>=b
not(a<b)
Unlike C, you should have multiple implementations of
<
and==
:
operator operands implementation ==
both same naïve ==
mixed false if not in common range <
both unsigned naïve <
both signed invert sign bits then as per unsigned <
signed+unsigned true if first operand is negative or second operand exceeds max_signed <
unsigned+signed false if first operand exceeds max_signed or second operand is negative For signed-operand operator unsigned-operand, the
==
and>
operators should return false if the signed operand is negative or the unsigned operand exceeds signed_max; while the reverse applies to the<
operator.The
<
and>
operators will need separate implementations for signed and unsigned.Note that you don't need to implement
!=
,>=
and<=
because they're the logical inverses of==
,<
and>
respectively.