I have just a little theoretical curiosity. The ==
operator in C returns 1
in case of positive equality, 0
otherwise. My knowledge of assembly is very limited. However I was wondering if it could be possible, theoretically, to implement a new operator that returns ~0
in case of positive equality, 0
otherwise – but at one condition: it must produce the same number of assembly instructions as the ==
operator. It's really just a theoretical curiosity, I have no practical uses in mind.
EDIT
My question targets x86 CPUs, however I am very curious to know if there are architectures that natively do that.
SECOND EDIT
As Sneftel has pointed out, nothing similar to the SETcc
instructions [1] – but able to convert flag register bits into 0
/~0
values (instead of the classical 0
/1
) – exists. So the answer to my question seems to be no.
THIRD EDIT
A little note. I am not trying to represent a logical true
as ~0
, I am trying to understand if a logical true can also be optionally represented as ~0
when needed, whithout further effort, within a language that already normally represents true
as 1
. And for this I had hypothized a new operator that “returns” numbers, not booleans (the natural logical true
“returned” by ==
remains represented as 1
) – otherwise I would have asked whether ==
could be re-designed to “return” ~0
instead of 1
. You can think of this new operator as half-belonging to the family of bitwise operators, which “return” numbers, not booleans (and by booleans I don't mean boolean data types, I mean anything outside of the number pair 0
/1
, which is what a boolean is intended in C as a result of a logical operation).
I know that all of this might sound futile, but I had warned: it is a theoretical question.
However here my question seems to be addressed explicitly:
Some languages represent a logical one as an integer with all bits set. This representation can be obtained by choosing the logically opposite condition for the
SETcc
instruction, then decrementing the result. For example, to test for overflow, use theSETNO
instruction, then decrement the result.
So it seems there is no direct instruction, since using SETNE
and then decrementing means adding one more instruction.