Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:39:10+00:00 2026-06-15T23:39:10+00:00

My understanding is that & is the bitwise AND operator. So I would expect

  • 0

My understanding is that & is the bitwise AND operator. So I would expect it to have no meaning when applied to logicals. However, I see that:

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

and so on. Likewise for the other bitwise operators.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:39:11+00:00Added an answer on June 15, 2026 at 11:39 pm

    So, why do these operators even accept logical operands?

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

    For details, see PEP 285.

    Specifically:

          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 how bool & bool returns a bool whereas bool & non-bool inherit’s int‘s behaviour (i.e. returns an int).

    Here are some examples demonstrating 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 behaviour does not apply to arithmetic operators. Those just use int‘s behaviour:

    In [15]: True + 0
    Out[15]: 1
    
    In [16]: True + False
    Out[16]: 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My understanding is that when using && as an mathematical operation in Java, if
My understanding of C# says (thanks to Jeff Richter & Jon Skeet) that assignment
It is my understanding that two unequal objects can have the same hashcode. How
I have the understanding that using /../ in a file location in bash will
It's my understanding that in C/C++ bitwise operators are supposed to be endian independent
I have an NSString that I would like to search numerically. It seems that
Understanding that I should probably just dig into the source to come up with
It's my understanding that in Spring, all objects are treated by default as singletons.
It's my understanding that all three of these lines below should return an ARRAY
It is my understanding that a texture atlas is basically a single texture that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.