Boolean Comparison Operators

In C ++, there are logical operators &&, ||, !that corresponds to the compound Venn0001.svg, disjunction Venn0111.svg, negation Venn10.svg, respectively.

But I noticed that the comparison operators ==, !=, <, >, <=, >=can also be used for Boolean! Given that Pthey Qare Boolean:

P == Qis biconversion Venn1001.svg,

P != Qis an exceptional disjunction Venn0110.svg,

P < Qis the reverse non-specific Venn0010.svg,

P > Qis a non-implication Venn0100.svg,

P <= Qis the implication Venn1011.svg,

And P >= Q- the reverse implication Venn1101.svg.

My questions:

  • ?

  • - , ( )?

+4
2

?

, - , P Q , , , .

, P < Q !P && Q: Q, , P true. .

- , ( )?

, , , , , ( - ):

if ((a == null) != (b == null))
  throw "a and b must either both be null, or both be non-null";

^. , .

+4

, , . :

bool biconditional(bool a, bool b)
{
    return (a && b) || (!a && !b);
}

bool biconditional_trick(bool a, bool b)
{
    return a == b;
}

:

biconditional(bool, bool):
        mov     eax, esi
        xor     eax, 1
        xor     eax, edi
        ret
biconditional_trick(bool, bool):
        cmp     dil, sil
        sete    al
        ret

gcc 5.3 > Compiler Explorer -O3 -Wall -fno-verbose-asm -march=haswell.

, 1 . , gcc . , , : https://gcc.gnu.org/lists.html

: : , . , , bool bool:

bool fa();
bool fb();

bool biconditional_with_function()
{
    return (fa() && fb()) || (!fa() && !fb());
}

bool biconditional_with_function_trick()
{
    return fa() == fb();
}

:

biconditional_with_function():
        sub     rsp, 8
        call    fa()
        test    al, al
        je      .L7
        call    fb()
        test    al, al
        jne     .L10
.L7:
        call    fa()
        mov     edx, eax
        xor     eax, eax
        test    dl, dl
        je      .L14
.L10:
        add     rsp, 8
        ret
.L14:
        call    fb()
        add     rsp, 8
        xor     eax, 1
        ret
biconditional_with_function_trick():
        push    rbx
        call    fa()
        mov     ebx, eax
        call    fb()
        cmp     bl, al
        pop     rbx
        sete    al
        ret

, , biconditional_with_function, , , true. , , fa() fb() , , . , , :

bool biconditional_with_function_rewritten()
{
    bool a = fa();
    bool b = fb();
    return (a && b) || (!a && !b);
}

:

biconditional_with_function_rewritten():
        push    rbx
        call    fa()
        mov     ebx, eax
        call    fb()
        xor     eax, 1
        xor     eax, ebx
        pop     rbx
        ret

, , 1 , "" .

, GCC , <:

bool fa();
bool fb();

bool converse_nonimplication()
{
    return !fa() && fb();
}

bool converse_nonimplication_trick()
{
    return fa() < fb();
}

:

converse_nonimplication():
        sub     rsp, 8
        call    fa()
        test    al, al
        je      .L5
        xor     eax, eax
        add     rsp, 8
        ret
.L5:
        add     rsp, 8
        jmp     fb()
converse_nonimplication_trick():
        push    rbx
        call    fa()
        mov     ebx, eax
        call    fb()
        cmp     al, bl
        pop     rbx
        seta    al
        ret
+2

Source: https://habr.com/ru/post/1657902/