#15 Python Tutorial for Beginners | Python BitWise Operators
- Get link
- Other Apps
#15 Python Tutorial for Beginners | Python BitWise Operators
Welcome, and in this series on Python, we will be focusing on bitwise operators. Bitwise operators are used to manipulate binary numbers, which can be useful when working with money formats or other applications. Let's dive right in!
The Complement Operator (~)
The tilde (~) symbol is used to represent the complement operator in Python. The complement of a binary number is obtained by flipping all the bits (0s become 1s and 1s become 0s). For example, the complement of 12 is -13.
Example:
~12 = -13
To understand why the complement of 12 is -13, we need to understand the concept of two's complement. In computer systems, negative numbers are stored in two's complement form. To find the two's complement of a number, we find the one's complement (flip all the bits) and then add 1 to the result.
Example:
12 in binary: 1100Complement of 12: 0011Two's complement of 12: 1101 (which is -13 in decimal)
Try different numbers and calculate their complements to get a better understanding of how this operator works!
Bitwise AND (&)
The ampersand (&) symbol is used to represent the bitwise AND operator in Python. The AND operator compares each bit of the two numbers and returns 1 if both bits are 1, otherwise it returns 0.
Example:
12 & 13 = 12
When we perform a bitwise AND operation on the binary representations of 12 and 13, we get the result 12. Try performing this operation with different numbers to see how it works!
Bitwise OR (|)
The pipe (|) symbol is used to represent the bitwise OR operator in Python. The OR operator compares each bit of the two numbers and returns 1 if at least one bit is 1, otherwise it returns 0.
Example:
12 | 13 = 13
When we perform a bitwise OR operation on the binary representations of 12 and 13, we get the result 13. Try performing this operation with different numbers to see how it works!
Bitwise XOR (^)
The caret (^) symbol is used to represent the bitwise XOR operator in Python. The XOR operator compares each bit of the two numbers and returns 1 if the bits are different, otherwise it returns 0.
Example:
12 ^ 13 = 1
When we perform a bitwise XOR operation on the binary representations of 12 and 13, we get the result 1. Try performing this operation with different numbers to see how it works!
Left Shift (<<)
The left shift operator (<<) is used to shift the bits of a number to the left by a specified number of positions. The shifted bits are filled with zeros.
Example:
10 << 2 = 40
When we perform a left shift operation on the binary representation of 10, shifting it 2 positions to the left, we get the result 40. Try performing this operation with different numbers and shift amounts to see how it works!
Right Shift (>>)
The right shift operator (>>) is used to shift the bits of a number to the right by a specified number of positions. The shifted bits are filled based on the sign of the original number.
Example:
10 >> 2 = 2
When we perform a right shift operation on the binary representation of 10, shifting it 2 positions to the right, we get the result 2. Try performing this operation with different numbers and shift amounts to see how it works!
I hope you find this series on bitwise operators helpful! If you have any questions or suggestions for future topics, feel free to leave a comment below.
https://www.highwaycpmrevenue.com/ka9ix7e0t?key=4807923f98c12e085b5d0cf16291fdec
How would you rate this summary?
- Get link
- Other Apps
Comments
Post a Comment