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
Answer from paxdiablo on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_operators_bitwise.asp
Python Bitwise Operators
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... Then the & operator compares the bits and returns 0010, which is 2 in decimal.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-bitwise-operators
Python Bitwise Operators - GeeksforGeeks
This means it inverts all bits in the value, transforming 0 bits to 1 and 1 bits to 0. Example: Take one-bit value X, where X = 10= (1010)2 Take Bitwise NOT of X. ... These operators are used to shift the bits of a number left or right thereby ...
Published: August 11, 2026
Discussions

python - Bitwise operation and usage - Stack Overflow
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: ... 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 ... More on stackoverflow.com
🌐 stackoverflow.com
[deleted by user]
Bitwise operator are for people doing bitwise operations, if you're not manipulating binary values of numbers, you don't need them. They exist for the people that do. Logical operators are for anyone that needs a true/false condition evaluated. Which is pretty much every single programmer. Use and and or in your if statements, and & and | in your math expressions. Note that sometimes & and | are overloaded (for example with sets). Those aren't bitwise operators, they're unrelated features of those classes that just use the same symbol. More on reddit.com
🌐 r/learnpython
17
1
May 15, 2022
What is Bitwise? (Python)
"Bitwise" means "for each bit." Numbers can be expressed as a binary number. For example, 40 is the binary number "101000", 41 is "101001", 42 is the binary number "101010", etc. You can think of each of those bits as either being TRUE (1) or FALSE (0). Operators like AND (&), XOR (^), and OR (|) take in two boolean (true or false) values and return true or false. TRUE & TRUE is TRUE, TRUE & FALSE is FALSE, etc. A bitwise operation takes each bit of two numbers (the first bit of the first number vs the first bit of the second number, and so on), runs them through some operation, and produces a number that is the result of that. For example: a = 40 (101000) b = 10 (001010) c= a ^ b a | b | a ^ b (exclusive or) | 1 | 0 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 so c is the binary "100010", which is 34. More on reddit.com
🌐 r/learnprogramming
11
2
March 27, 2023
How can I perform bitwise operations in python exactly how javascript would?
javascript uses 32 (signed) bit integers, meaning the maximum integer it can handle is 231 - 1, and the minimum is -(231 - 1), if an operation results in an integer exceeding these limits, the result gets truncated. In Python you can truncate integers to 32 bit by performing bitwise AND with 0xFFFFFFFF def truncate_to_32_bit(num): return num & 0xFFFFFFFF If you want to also handle the sign bit: def truncate_to_32_bit_signed(num): # Truncate the number to 32 bits truncated_num = num & 0xFFFFFFFF # If the sign bit is set, convert to a negative integer if truncated_num & (1 << 31): truncated_num -= 1 << 32 return truncated_num More on reddit.com
🌐 r/learnpython
3
3
May 7, 2024
People also ask

Are bitwise operators exclusive to integers in Python
divYes bitwise operators primarily operate on integers in Python While you can apply them to other numeric types the results may not be as expected and its important to consider the binary representation of the datadiv
🌐
scholarhat.com
scholarhat.com › home › tutorials › python › bitwise operators in pyth..
Bitwise Operators in Python: Types and Examples
When would I use bitwise OR in Python?
Use bitwise OR (|) when you need to combine bits. It sets the corresponding result bit to 1 if any of the bits being compared is 1. For example, 5 | 3 results in 7.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › bitwise operators in python
Bitwise Operator in Python: Everything You Need to Know
What is the significance of the bitwise NOT operator in Python?
The bitwise NOT operator in Python is useful for inverting the binary representation of numbers, which is crucial in systems programming, encryption, and working with negative numbers in two's complement.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › bitwise operators in python
Bitwise Operator in Python: Everything You Need to Know
🌐
Python
wiki.python.org › moin › BitwiseOperators
BitwiseOperators - Python Wiki
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.
🌐
WsCube Tech
wscubetech.com › resources › python › bitwise-operators
Bitwise Operators in Python: All Types With Examples
August 6, 2026 - Learn about bitwise operators in Python, their types, uses, and examples. Master AND, OR, XOR, NOT, and other operations with easy-to-follow explanations.
🌐
Real Python
realpython.com › python-bitwise-operators
Bitwise Operators in Python – Real Python
July 28, 2025 - It’s sort of like looking at an unbounded stream of bits through a fixed-length window. There are a few tricks that let you do this in Python. For example, you can apply a bitmask with the bitwise AND operator:
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

Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › python › bitwise_operators_example.htm
Python - Bitwise Operators
March 12, 2021 - We now perform the bitwise XOR manually. ... The int() function shows 00110001 to be 49. ... This operator is the binary equivalent of logical NOT operator. It flips each bit so that 1 is replaced by 0, and 0 by 1, and returns the complement ...
🌐
ScholarHat
scholarhat.com › home › tutorials › python › bitwise operators in pyth..
Bitwise Operators in Python: Types and Examples
September 11, 2025 - Bitwise operators in Python are special operators that are used to perform operations directly on the individual bits (binary digits) of integer numbers. Numbers are first broken down into their binary form (1s and 0s), and operations such as AND, OR, XOR, shifts, and more are performed. They’re super handy when you want to optimize low-level operations, like working with flags, cryptography, or even graphics programming.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › bitwise operators in python
Bitwise Operator in Python: Everything You Need to Know
September 14, 2024 - It sets the corresponding result bit to 1 if any of the bits being compared is 1. For example, 5 | 3 results in 7. Bitwise operators in Python with examples allow for faster operations than traditional arithmetic, especially when dealing with low-level tasks like shifting bits, setting flags, or handling binary data.
🌐
DataFlair
data-flair.training › blogs › python-bitwise-operators
Python Bitwise Operators with Syntax and Example - DataFlair
April 28, 2026 - Here, 110 is binary for 6, and 101 for 5. &-ing them, we get 100, which is binary for 4. Now let’s discuss Python Bitwise OR (|) Operator · Compared to &, this one returns 1 even if one of the two corresponding bits from the two operands is 1. ... This is the same as the following. ... Let’s see some more examples.
🌐
Edureka
edureka.co › blog › bitwise-operators-in-python
Bitwise Operators In Python | Program For Bitwise Operator | Edureka
September 16, 2019 - Bitwise AND Operation = a & b. Analysis: 00001001 & 01000001 = 00000001 = 1 · Bitwise OR Operation = a | b. Analysis: 00001001 | 01000001 = 01001001 = 73 · Bitwise Exclusive OR Operation in Python = a ^ b.
🌐
Scientech Easy
scientecheasy.com › home › blog › bitwise operators in python
Bitwise Operators in Python - Scientech Easy
July 6, 2026 - Let’s write a Python program in which we will perform a bitwise XOR operation between two numbers, 2 and 6. A = 2 B = 6 result = A ^ B print("Result of (A ^ B): ", result) ... In the preceding example, the binary conversion of 2 is 0010, and ...
🌐
RichardKilleen
richardkilleen.co.uk › home › blog › python for network engineers blog 17: python bitwise operators
Python for Network Engineers Blog 17: Python Bitwise Operators - RichardKilleen
September 15, 2025 - Right so python bitwise operators work directly on the binary representation of numbers. That means they manipulate individual bits rather than treating numbers as whole values. Simple enough concept. ... # The six bitwise operators with network examples network_address = 192 # 11000000 in binary subnet_mask = 255 # 11111111 in binary # Bitwise AND (&) result_and = network_address & subnet_mask print("Bitwise AND:", result_and) # 192 # Bitwise OR (|) result_or = network_address | 63 print("Bitwise OR:", result_or) # 255 # Bitwise XOR (^) result_xor = network_address ^ 63 print("Bitwise XOR:", result_xor) # 63 # Bitwise NOT (~) result_not = ~network_address print("Bitwise NOT:", result_not) # -193 # Left shift (<<) result_left = network_address << 1 print("Left shift:", result_left) # 384 # Right shift (>>) result_right = network_address >> 1 print("Right shift:", result_right) # 96
🌐
Unstop
unstop.com › home › blog › python bitwise operators with detailed code examples
Python Bitwise Operators With Detailed Code Examples
June 6, 2025 - The tilde symbol (~) represents the unary bitwise operator, NOT in Python. The result variable stores the output of Bitwise NOT operation. Example: Let’s take an integer a = 12.
🌐
TOOLSQA
toolsqa.com › python › python-bitwise-operators
Python Bitwise Operators explained With examples
August 6, 2021 - For example, in sequence 111011, if you want to know the bit which has 0. You can do that using the bitwise operator (we will cover that later on how to do that). Error checking: Bitwise operators are used extensively in error checking.
🌐
Uncodemy
uncodemy.com › blog › python-bitwise-operators-with-examples
Python Bitwise Operators with Examples
Practical Use: XOR is used for toggling bits, simple encryption, finding unique elements, and swapping variables without temporary storage. The NOT operator flips all bits in a number. It changes 1s to 0s and 0s to 1s. This is called the bitwise complement. ... Note: The result might seem confusing because Python uses two's complement representation for negative numbers.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-bitwise-operators
Python Bitwise Operators | DigitalOcean
Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
StudySmarter
studysmarter.co.uk › python bitwise operators
Python Bitwise Operators: Definition & Examples
In Python, these operators are used on integers, which are binary representations in memory. The main bitwise operators available in Python are: ... Bitwise AND (&): This operator takes two numbers as operands and does a binary AND operation. Every digit from the first operand is compared with ...
🌐
Javatpoint
javatpoint.com › python-bitwise-operators
Python Bitwise Operators - Javatpoint
Python Bitwise Operators with Python with python, tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, operators, etc.
🌐
Intellipaat
intellipaat.com › home › blog › python bitwise operators
Python Bitwise Operators (With Examples)
October 17, 2025 - This is important for binary manipulation in Python for negative values. ... The ~ operator inverts all bits and returns -(n + 1). Example: ~2 gives -3, because all bits are flipped and adjusted in two’s complement form.