๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ BitwiseOperators
BitwiseOperators - Python Wiki
These are Python's bitwise operators. All of these operators share something in common -- they are "bitwise" operators. That is, they operate on numbers (normally), but instead of treating that number as if it were a single value, they treat it as if it were a string of bits, written in two's complement binary...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_operators_bitwise.asp
Python Bitwise Operators
Python Examples Python Compiler ... Python Training ยท โฎ Previous Next โฏ ยท Share Link Copied ยท Bitwise operators are used to compare (binary) numbers: The & operator compares each bit and set it to 1 if both are 1, otherwise ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-bitwise-operators
Python Bitwise Operators - GeeksforGeeks
Bitwise AND (&) operator takes two equal-length bit patterns as parameters. The two-bit integers are compared. If the bits in the compared positions of the bit patterns are 1, then the resulting bit is 1. If not, it is 0. Example: Take two-bit values X and Y, where X = 7= (111)2 and Y = 4 = (100)2 Take Bitwise and of both X & Y ยท Note: Here, (111)2 represent binary number.
Published: August 11, 2026
๐ŸŒ
Real Python
realpython.com โ€บ python-bitwise-operators
Bitwise Operators in Python โ€“ Real Python
July 28, 2025 - Since integers in Python can have an infinite number of bits, the sign bit doesnโ€™t have a fixed position. In fact, thereโ€™s no sign bit at all in Python! Most of the bitwise operators are binary, which means that they expect two operands to work with, typically referred to as the left operand and the right operand.
๐ŸŒ
Python Data Science Handbook
jakevdp.github.io โ€บ WhirlwindTourOfPython โ€บ 04-semantics-operators.html
Basic Python Semantics: Operators | A Whirlwind Tour of Python
In the previous section, we began ... By the end of this section, you'll have the basic tools to begin comparing and operating on data in Python. Python implements seven basic binary arithmetic operators, two of which can double as unary operators....
๐ŸŒ
NetworkLessons
networklessons.com โ€บ home โ€บ python โ€บ python bitwise operators
Python Bitwise Operators
The Python bitwise operators can compare binary numbers. This lesson explains how to use the AND, OR, XOR, NOT, and SHIFT operators.
Published: April 3, 2020
๐ŸŒ
University of Vermont
uvm.edu โ€บ ~cbcafier โ€บ cs2210 โ€บ content โ€บ 03_bits_bytes_words โ€บ bitwise_ops_python.html
Bitwise Operations with Python โ€“ Clayton Cafiero
October 13, 2025 - A bitwise operation is an operation that is applied to individual bits of some object. >> is the bitshift right operator and << is the bitshift left operator. These are binary infix operators that take the value to be shifted on the left, and the number of positions to be shifted on the right.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ bitwise_operators_example.htm
Python - Bitwise Operators
March 12, 2021 - Python has six bitwise operators - &, |, ^, ~, << and >>. All these operators (except ~) are binary in nature, in the sense they operate on two operands.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Bitwise Operators in Python (AND, OR, XOR, NOT, SHIFT) | note.nkmk.me
May 6, 2023 - Python provides the bitwise operators, &(AND), |(OR), ^(XOR), ~(NOT, invert), <<(LEFT SHIFT), >>(RIGHT SHIFT). ... For more information about converting binary, octal, and hexadecimal numbers and strings using bin(), oct(), hex(), and format(), ...
Find elsewhere
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ python bitwise operators with detailed code examples
Python Bitwise Operators With Detailed Code Examples
June 6, 2025 - Python Bitwise operators, including AND, OR, NOT, XOR, Left-shift, and Right-shift, facilitate the manipulation of data/ operands at the binary bit level.
๐ŸŒ
DataFlair
data-flair.training โ€บ blogs โ€บ python-bitwise-operators
Python Bitwise Operators with Syntax and Example - DataFlair
April 28, 2026 - Python Bitwise Operators take one to two operands, and operate on it/them bit by bit, instead of whole. ... Binary process: These operators work on numbers by converting them into binary language(0s and 1s).
๐ŸŒ
Scientech Easy
scientecheasy.com โ€บ home โ€บ blog โ€บ bitwise operators in python
Bitwise Operators in Python - Scientech Easy
July 6, 2026 - Then, bitwise operators convert binary output to a decimal number and give the output in the decimal number. In bitwise operations, the Python interpreter internally converts the integer numbers into the binary number system that is represented by two digits, 0 or 1.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ bitwise operator in python
Bitwise Operator in Python | Scaler Topics
December 27, 2023 - Bitwise Operator in Python can be defined as performing the calculations on integers by first converting the integers to its binary format and then simply performing the operations on it bit by bit as the name suggests (bitwise operators).
๐ŸŒ
StudySmarter
studysmarter.co.uk โ€บ python bitwise operators
Python Bitwise Operators: Definition & Examples
Bitwise XOR (^): This operator takes two numbers as operands and does a binary XOR operation. It gives a 1 if the bits are different; otherwise, it results in 0. ... Bitwise NOT (~): This operator takes a single number and inverts all the bits, turning zeroes into ones and vice versa. In Python, the Bitwise NOT operation inverts the bits and also takes the two's complement of the number.
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ python tutorial โ€บ python bitwise operators
Python Bitwise Operators
December 10, 2025 - Python Bitwise Operators (&, |, ^, ~, ) What Are Bitwise Operators? Python Bitwise operators perform operations on the binary representations of integers. Instead of working with the decimal numbers you see (like 10 or 5), these operators work ...
Top answer
1 of 16
176

Bitwise operators are operators that work on multi-bit values, but conceptually one bit at a time.

  • AND is 1 only if both of its inputs are 1, otherwise it's 0.
  • OR is 1 if one or both of its inputs are 1, otherwise it's 0.
  • XOR is 1 only if exactly one of its inputs are 1, otherwise it's 0.
  • NOT is 1 only if its input is 0, otherwise it's 0.

These can often be best shown as truth tables. Input possibilities are on the top and left, the resultant bit is one of the four (two in the case of NOT since it only has one input) values shown at the intersection of the inputs.

AND | 0 1     OR | 0 1     XOR | 0 1    NOT | 0 1
----+-----    ---+----     ----+----    ----+----
 0  | 0 0      0 | 0 1       0 | 0 1        | 1 0
 1  | 0 1      1 | 1 1       1 | 1 0

One example is if you only want the lower 4 bits of an integer, you AND it with 15 (binary 1111) so:

    201: 1100 1001
AND  15: 0000 1111
------------------
 IS   9  0000 1001

The zero bits in 15 in that case effectively act as a filter, forcing the bits in the result to be zero as well.

In addition, >> and << are often included as bitwise operators, and they "shift" a value respectively right and left by a certain number of bits, throwing away bits that roll of the end you're shifting towards, and feeding in zero bits at the other end.

So, for example:

1001 0101 >> 2 gives 0010 0101
1111 1111 << 4 gives 1111 0000

Note that the left shift in Python is unusual in that it's not using a fixed width where bits are discarded - while many languages use a fixed width based on the data type, Python simply expands the width to cater for extra bits. In order to get the discarding behaviour in Python, you can follow a left shift with a bitwise and such as in an 8-bit value shifting left four bits:

bits8 = (bits8 << 4) & 255

With that in mind, another example of bitwise operators is if you have two 4-bit values that you want to pack into an 8-bit one, you can use all three of your operators (left-shift, and and or):

packed_val = ((val1 & 15) << 4) | (val2 & 15)
  • The & 15 operation will make sure that both values only have the lower 4 bits.
  • The << 4 is a 4-bit shift left to move val1 into the top 4 bits of an 8-bit value.
  • The | simply combines these two together.

If val1 is 7 and val2 is 4:

                val1            val2
                ====            ====
 & 15 (and)   xxxx-0111       xxxx-0100  & 15
 << 4 (left)  0111-0000           |
                  |               |
                  +-------+-------+
                          |
| (or)                0111-0100
2 of 16
47

One typical usage:

| is used to set a certain bit to 1

& is used to test or clear a certain bit

  • Set a bit (where n is the bit number, and 0 is the least significant bit):

    unsigned char a |= (1 << n);

  • Clear a bit:

    unsigned char b &= ~(1 << n);

  • Toggle a bit:

    unsigned char c ^= (1 << n);

  • Test a bit:

    unsigned char e = d & (1 << n);

Take the case of your list for example:

x | 2 is used to set bit 1 of x to 1

x & 1 is used to test if bit 0 of x is 1 or 0

๐ŸŒ
PyTutorial
pytutorial.com โ€บ python-binary-operators-guide-for-beginners
PyTutorial | Python Binary Operators Guide for Beginners
April 4, 2026 - Learn how Python binary operators like +, -, *, /, and, or work with practical code examples for arithmetic, comparison, and logical operations.
๐ŸŒ
TOOLSQA
toolsqa.com โ€บ python โ€บ python-bitwise-operators
Python Bitwise Operators explained With examples
August 6, 2021 - In the following calculation, we are operating 100 and 101 using & bitwise operator in Python. You already know that to represent a binary number, we prefix it with 0b.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-bitwise-operators
Python Bitwise Operators
March 26, 2025 - Python Bitwise operators are used to perform bit operations. The Bitwise operators convert decimal into binary values, seq of bits 0100, 1100.