We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

Python Bitwise AND and OR Operators

Lane Wagner
Lane WagnerBoot.dev co-founder and backend engineer

Last published

Table of Contents

Bitwise operators are similar to logical operators, but instead of operating on boolean values, they apply the same logic to all the bits in a value by column. The two operators covered here are & (AND) and | (OR).

All the content from our Boot.dev courses are available for free here on the blog. This one is from the "Computing" chapter of Learn Python for Beginners. If you want to try the far more immersive version of the course, do check it out!

Binary Numbers in Python

Binary numbers are just "base 2" numbers. They work the same way as "normal" base 10 numbers, but with two symbols instead of ten.

  • Base-2 (binary) symbols: 0 and 1
  • Base-10 (decimal) symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Click to play video

Each digit's place value is double the place to its right. In a 4-digit binary number, the place values are:

  • Eights
  • Fours
  • Twos
  • Ones

For example, 0101 means:

  • 0 eights
  • 1 four
  • 0 twos
  • 1 one

So 0101 is 5 in decimal.

Try changing the bits:

Interactive example available with JavaScript enabled.

You can write an integer in Python using binary syntax using the 0b prefix:

print(0b0001)
# Prints 1

print(0b0101)
# Prints 5

Leading 0s are often added for visual consistency but do not change the value of a binary number.

Bitwise AND (&)

Say you had the numbers 5 and 7 represented in binary. You could perform a bitwise AND operation and the result would be 5.

  • 0101 is 5
  • 0111 is 7
0101
&
0111
=
0101

Interactive example available with JavaScript enabled.

Click to play video

A 1 in binary is the same as True, while 0 is False. So really a bitwise operation is just a bunch of logical operations that are completed in tandem by column.

Ampersand & is the bitwise AND operator in Python. "AND" is the name of the bitwise operation, while ampersand & is the symbol for that operation. For example, 5 & 7 = 5, while 5 & 2 = 0.

binary_five = 0b0101
binary_seven = 0b0111
print(binary_five & binary_seven)
# 5

Bitwise OR (|)

The bitwise "or" operator is similar to the bitwise "and" operator in that it works on binary rather than boolean values. However, the bitwise "or" operator "or"s the bits together.

0101
|
0111
=
0111

Interactive example available with JavaScript enabled.

When two binary numbers are "or"ed together, the result has a 1 in any place where either of the input numbers has a 1 in that place.

| is the bitwise "or" operator in Python. 5 | 7 = 7 and 5 | 2 = 7 as well!

Using Bitwise Operators for Permissions

Sometimes applications store user permissions as binary values. If you have 4 different permissions a user can have, then you can store that as a 4-digit binary number, and if a certain bit is present, you know the permission is enabled. This can be a lot more efficient than storing entire strings.

can_read = 0b100
can_write = 0b010
can_execute = 0b001

user_perms = can_read | can_write  # 0b110
has_read = user_perms & can_read   # 0b100 (truthy — has permission)

Use bitwise AND to check whether a specific permission is enabled. Use bitwise OR to combine permissions.

Convert a Binary String to an Integer

The built-in int() function can convert a binary string to an integer. It takes a second argument that specifies the base of the number (binary is base 2). For example:

# this is a binary string
binary_string = "100"

# convert binary string to integer
num = int(binary_string, 2)
print(num)
# 4

If you're curious about how other bitwise operations show up in the real world, read why XOR is important in cryptography.

Frequently Asked Questions

What do bitwise AND and OR do in Python?

Bitwise AND returns a 1 where both input bits are 1. Bitwise OR returns a 1 where either input bit is 1.

What is the difference between and and & in Python?

and is a boolean operator that works with truth values and short-circuits. The & operator applies AND to each corresponding bit of integer operands.

How do you write a binary number in Python?

Prefix the binary digits with 0b. For example, 0b0101 is the integer 5. Leading zeroes do not change the value.

How do you convert a binary string to an integer in Python?

Pass the string and base 2 to int(). For example, int("101", 2) returns 5.

What are bitwise AND and OR used for?

They can check and combine compact permission flags stored as binary values.