What do binary operators mean when applied to logic?

I understand that & is the bitwise operator of I. Therefore, I expect this to not make sense when applied to logic. However, I see that:

 >>> False & False False >>> False & True False >>> True & True True 

etc. Similarly for other bitwise operators.

So why do these operators accept logical operands? And where can I find documentation that explains this? I searched for him, but could not find an explanation.

+4
source share
3 answers

So why do these operators accept logical operands?

bool subclasses int and overrides __and__() , etc., to return bool operands for bool .

See PEP 285 for details.

In particular:

  6) Should bool inherit from int?

        => Yes

        In an ideal world, bool might be better implemented as a
        separate integer type that knows how to perform mixed-mode
        arithmetic.  However, inheriting bool from int eases the
        implementation enormously (in part since all C code that calls
        PyInt_Check () will continue to work - this returns true for
        subclasses of int).  Also, I believe this is right in terms of
        substitutability: code that requires an int can be fed a bool
        and it will behave the same as 0 or 1. Code that requires a
        bool may not work when it is given an int;  for example, 3 & 4
        is 0, but both 3 and 4 are true when considered as truth
        values.

and

  class bool(int): def __and__(self, other): if isinstance(other, bool): return bool(int(self) & int(other)) else: return int.__and__(self, other) __rand__ = __and__ def __or__(self, other): if isinstance(other, bool): return bool(int(self) | int(other)) else: return int.__or__(self, other) __ror__ = __or__ def __xor__(self, other): if isinstance(other, bool): return bool(int(self) ^ int(other)) else: return int.__xor__(self, other) __rxor__ = __xor__ 

Note that bool & bool returns bool , while bool & non-bool inherits int behavior (i.e. returns int ).

Here are a few examples that demonstrate these properties:

 In [12]: isinstance(True, int) Out[12]: True In [13]: True & True Out[13]: True In [14]: True & 1 Out[14]: 1 

The above behavior does not apply to arithmetic operators. They just use int behavior:

 In [15]: True + 0 Out[15]: 1 In [16]: True + False Out[16]: 1 
+8
source

Logicals, aka boolean, simply represent one bit either on, and, of course, bitwise operations work on them. What would make you expect that bitwise operations will not work with single bits?

You are inverting the inheritance of logical operations. The main case of bitwise operations is single-bit variables, such as logical ones. Bitwise operations with large values ​​are just an extended one-bit operation application.

0
source

In Python 3.x, True and False are keywords and will always be 1 and 0.

Under normal circumstances in Python 2 and always in Python 3:

The False object is of type bool, which is a subclass of int:

  object | int | bool 
0
source

All Articles