Bitwise Operator in Python

Bitwise Operator might sound a little technical, but trust me, they are simple once you understand how they work. So today you will learn about bitwise operator, by the end of this blog, you will understand what bitwise operators are and how to use them in Python.

Bitwise Operator in Python

What are Binary Numbers?

Before learning, You know how computers use 0s and 1s to represent everything? Computers use 0s and 1s to represent all kinds of information. This system is called binary code. Each 0 or 1 is a bit (the smallest unit of data in a computer), and when you put many bits together, they can represent numbers, letters, images, and more. Computer only understand these two numbers, which mean off (0) and on (1).

Computers work by turning tiny switches inside them on and off. These switches are called transistors. By using many of these switches together, computers use binary numbers to do everything.

Converting Integer to Binary

Now, Let me tell you how to change a regular number or integer (like 13) into binary. The computer repeatedly divide the number by 2 and writes down the remainder.

  • Step 1: Divide the number by 2.
  • Step 2: Write down the remainder (it will be either 0 or 1).
  • Step 3: Use the quotient (the result of the division) for the next division.
  • Step 4: Repeat this process until the quotient is 0.
  • Step 5: The binary number is the remainders read from bottom to top.

For 13

  • 13 ÷ 2 = 6, remainder = 1
  • 6 ÷ 2 = 3, remainder = 0
  • 3 ÷ 2 = 1, remainder = 1
  • 1 ÷ 2 = 0, remainder = 1

Now, if we write the remainders from bottom to top, we get: 1101. So, 13 in binary is 1101.

Converting Binary to Integer

To turn a binary number (like 1110) back into an integer, the computer uses place values (just like in regular numbers).

In binary, each place represents powers of 2: From right to left, the places are: 2⁰, 2¹, 2², 2³, etc.

Example:

Start from the right:

  1. The rightmost digit (1) is in the 2⁰ place (which is 1).
  2. The next digit (0) is in the 2¹ place (which is 2).
  3. The next digit (1) is in the 2² place (which is 4).
  4. The leftmost digit (1) is in the 2³ place (which is 8).

Now, we can calculate the value:

  • 1 × 2³ = 1 × 8 = 8
  • 1 × 2² = 1 × 4 = 4
  • 0 × 2¹ = 0 × 2 = 0
  • 1 × 2⁰ = 1 × 1 = 1

Now, add them all together:

8 + 4 + 0 + 1 = 13. So, the binary number 1101 is equal to the integer 13. Now, You are ready to learn bitwise operator.

What are Bitwise Operators?

Bitwise operators are special tools in programming that let you work with the binary representation of numbers. Bitwise operators perform operations on binary numbers, bit by bit. This means they look at each digit (or bit) of the numbers and do something with them. There are several bitwise operators in Python, and I am going explain the most important ones.

1. Bitwise AND (&)

The AND operator compares two bits. If both bits are 1, the result is 1. If either bit is 0, the result is 0.

Here is a simple truth table to help you understand:

ABA & B
000
010
100
111

Example:
If you have two numbers, say 12 and 5, their binary forms are:

  • 12 in binary: 1100
  • 5 in binary: 0101

When you apply the AND operator:

  1100
& 0101
------
  0100  (which is 4 in decimal)
Python

Hence, 12 & 5 = 4

2. Bitwise OR (|)

The OR operator also compares two bits. If at least one of the bits is 1, the result is 1. If both bits are 0, the result is 0.

Truth table:

ABA | B
000
011
101
111

Example:
Again, If you have two numbers, say 5 and 3, their binary forms are:

  • 5 in binary 0101
  • 3 in binary 0011

When apply the OR operator:

0101  
|  
0011  
----  
0111  (This is 7 in decimal)
Python

So, 5 | 3 = 7.

3. Bitwise NOT (~)

The NOT operator flips each bit—it changes 1 to 0 and 0 to 1.

Example:
suppose you have the number 5 (0101 in binary). Applying NOT (~) convert it to 1010.

However, Python uses something called Two’s Complement for negative numbers, so ~5 becomes -6. (Let me know if you want more detail on this in comment)

4. Bitwise XOR (^)

This one is different. The XOR operator compares two bits. If the bits are different, the result is 1. If they are the same, the result is 0.

Truth Table:

ABA ^ B
000
011
101
110

Example:
Using 12 and 5 again:

  • 12 in binary: 1100
  • 5 in binary: 0101

When you apply XOR:

  1100
^ 0101
------
  1001  (which is 9 in decimal)
Python

So, 12 ^ 5 = 9.

5. Left Shift (<<)

The left shift operator moves all bits to the left by a certain number of positions. When you shift bits to the left, you are moving each bit to a higher place value. When you shift bits to the left, you fill in the empty spaces on the right with zeros. So, if you shift 0001 (1) to the left, it becomes 0010 (2). If you shift it again, it becomes 0100 (4). Each time you left shift a number by 1 position, it’s like multiplying that number by 2.

Example:
If you have 12 (1100 in binary) and do a left shift by 1 (12 << 1):

  1100 << 1 = 11000 (which is 24 in decimal)
Python

So, 12 << 1 = 24.

6. Left Shift (<<)

The right shift operator moves all bits to the right by a certain number of positions. When you shift bits to the right, you are moving each bit to a lower place value.
For example, if you have the binary number 0010 (which is 2 in decimal), and you right shift it by 1 position, it becomes 0001 (which is 1 in decimal). When you shift bits to the right, the empty spaces on the left are usually filled with zeros. This is called a logical right shift. Each time you right shift a number by 1 position, it’s like dividing that number by 2.

Example:

  • 4 >> 1 (4 shifted right by 1) is 2 (4 / 2).
  • 4 >> 2 (4 shifted right by 2) is 1 (4 / 2 / 2).
Example:
a = 5  # In binary: 0101
b = 3  # In binary: 0011

# Bitwise AND
print(a & b)  # Output: 1 (binary: 0001)

# Bitwise OR
print(a | b)  # Output: 7 (binary: 0111)

# Bitwise NOT
print(~a)     # Output: -6 (binary: 1010, but in two's complement)

# Bitwise XOR
print(a ^ b)  # Output: 6 (binary: 0110)

# Left Shift
print(a << 1) # Output: 10 (binary: 1010)

# Right Shift
print(a >> 1) # Output: 2 (binary: 0010)
Python

Conclusion

And that’s it! You have learned about bitwise operators in Python. They might seem a little confusing at first, but with some practice, you will understand them better. Just remember, these operators work on the individual bits of numbers, and they can be really useful in programming.

If you have any questions or want to learn more about Python, feel free to ask.

Bitwise Operator in Python

13 divided by 2

1 / 4

How is the integer 13 represented in binary?

0101 OR 0011

2 / 4

What is the result of 5 | 3?

OR symbol

3 / 4

Which operator performs a bitwise OR operation?

Operate on binary bits

4 / 4

What do bitwise operators in Python work on?

Your score is

The average score is 0%

0%

Leave a Reply

Your email address will not be published. Required fields are marked *