Bitwise Operators and Assertion
Explore how bitwise operators allow manipulation of individual bits in Python and understand using assertion statements to check program assumptions dynamically. This lesson helps you debug by catching errors early and making your code more robust.
We'll cover the following...
Bitwise operators
Bitwise operators allow us to work with the individual bits of a byte. There are many bitwise operators available, as shown in the table below.
Operator | Name | Purpose |
| NOT (also called complement operator) | It converts 0 to 1 and 1 to 0. |
| LEFT SHIFT | Shift out the desired number of bits from left. |
| RIGHT SHIFT | Shift out the desired number of bits from right. |
| AND | Check whether a bit is on / off. Put off a particular bit. |
| OR | Put on a particular bit. |
| XOR | It toggles a bit. |
The following code shows the usage of bitwise operators:
Remember:
- If we perform the
&operation on anything with 0, it will become 0. - If we perform the
|operation on anything with 1, it will become 1. - If we perform the
^operation on 1 with 1, it