Bitwise Operators in Python Part 1

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 25:- Bitwise Operators in Python Part 1

In Python, bitwise operators are used to perform operations on individual bits of integers. These operators work at the binary level, manipulating the bits of numbers. Bitwise operators are useful when you need to work with low-level data representation and perform bitwise manipulations. There are six bitwise operators in Python:

  1. Bitwise AND: & Performs a bitwise AND operation between corresponding bits of two integers. Each bit in the result is set to 1 only if both corresponding bits in the operands are 1.

Example:

pythonCopy code

a = 10     # Binary: 1010 b = 7      # Binary: 0111 result = a & b print(result)   # Output: 2 (Binary: 0010)

  1. Bitwise OR: | Performs a bitwise OR operation between corresponding bits of two integers. Each bit in the result is set to 1 if at least one corresponding bit in the operands is 1.

Example:

pythonCopy code

a = 10     # Binary: 1010 b = 7      # Binary: 0111 result = a | b print(result)   # Output: 15 (Binary: 1111)

  1. Bitwise XOR: ^ Performs a bitwise XOR (exclusive OR) operation between corresponding bits of two integers. Each bit in the result is set to 1 if the corresponding bits in the operands are different.

Example:

pythonCopy code

a = 10     # Binary: 1010 b = 7      # Binary: 0111 result = a ^ b print(result)   # Output: 13 (Binary: 1101)

  1. Bitwise NOT: ~ Performs a bitwise NOT operation on an integer, inverting all its bits. This operator is unary, meaning it works on a single operand.

Example:

pythonCopy code

a = 10     # Binary: 1010 result = ~a print(result)   # Output: -11 (Binary: 1111 0101)

  1. Bitwise Left Shift: << Shifts the bits of an integer to the left by the specified number of positions. Zeros are added to the right.

Example:

pythonCopy code

a = 5      # Binary: 0101 result = a << 2 print(result)   # Output: 20 (Binary: 10100)

  1. Bitwise Right Shift: >> Shifts the bits of an integer to the right by the specified number of positions. Zeros are added to the left.

Example:

pythonCopy code

a = 20     # Binary: 10100 result = a >> 2 print(result)   # Output: 5 (Binary: 0101)

Bitwise operators are particularly useful in scenarios where you need to manipulate individual bits or perform binary-level operations. They are commonly used in embedded systems, networking protocols, and other low-level programming tasks.

4. Operators

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?