Welcome to the exciting world of Bitwise Operators! In this lesson, we'll dive deep into the fascinating world of bit manipulation using operators like &, |, ^, ~, <<, and >>. These operators are crucial in understanding and optimizing complex algorithms, making them a must-know for every developer. Let's get started!
Bitwise operators are binary operators that perform operations on individual bits (0s and 1s) within a number. They are essential in low-level programming, system programming, and algorithm optimization.
Before we delve into bitwise operators, it's important to understand binary numbers. A binary number is a number system that uses only two digits, 0 and 1. Each digit is called a bit.
Example: 1011 is a binary number, and its decimal equivalent is 11 (calculated as (1*2^3) + (0*2^2) + (1*2^1) + (1*2^0)).
Here's a quick overview of the six main bitwise operators:
& (Bitwise AND): Returns 1 only if both corresponding bits are 1.| (Bitwise OR): Returns 1 if either of the corresponding bits is 1.^ (Bitwise XOR): Returns 1 if exactly one of the corresponding bits is 1.~ (Bitwise NOT): Inverts all bits (1 becomes 0 and 0 becomes 1).<< (Bitwise Shift Left): Shifts bits to the left (multiplies by 2).>> (Bitwise Shift Right): Shifts bits to the right (divides by 2).&) š”The & operator compares each bit of the two operands and returns a 1 only if both bits are 1. Let's see an example:
# Example of bitwise AND
a = 60 # binary: 00111100
b = 13 # binary: 00001101
a & b # output: 12 (binary: 00001100)In the example above, the binary representation of a and b is shown, and the result of the bitwise AND operation between them is also displayed.
|) š”The | operator returns a 1 if either of the corresponding bits is 1. Let's see an example:
# Example of bitwise OR
a = 60 # binary: 00111100
b = 13 # binary: 00001101
a | b # output: 61 (binary: 00111101)In the example above, the binary representation of a and b is shown, and the result of the bitwise OR operation between them is also displayed.
^) š”The ^ operator returns a 1 if exactly one of the corresponding bits is 1. Let's see an example:
# Example of bitwise XOR
a = 60 # binary: 00111100
b = 13 # binary: 00001101
a ^ b # output: 49 (binary: 00110001)In the example above, the binary representation of a and b is shown, and the result of the bitwise XOR operation between them is also displayed.
~) š”The ~ operator inverts all bits of the number. Let's see an example:
# Example of bitwise NOT
a = 60 # binary: 00111100
~a # output: -61 (binary: 11000011)In the example above, the binary representation of the number 60 is shown, and the result of the bitwise NOT operation (inverting all bits) is also displayed. Note that the result is negative since the most significant bit (MSB) is set to 1.
<<) š”The << operator shifts all bits in the number to the left by a specified number of places. Let's see an example:
# Example of bitwise shift left
a = 5 # binary: 00000101
a << 2 # output: 20 (binary: 00010100)In the example above, the binary representation of the number 5 is shown, and the result of shifting all bits to the left by 2 places is also displayed.
>>) š”The >> operator shifts all bits in the number to the right by a specified number of places. Let's see an example:
# Example of bitwise shift right
a = 5 # binary: 00000101
a >> 2 # output: 1 (binary: 00000001)In the example above, the binary representation of the number 5 is shown, and the result of shifting all bits to the right by 2 places is also displayed.
What is the result of `(5 << 2) + (5 >> 2)`?
Write a Python program to check if a number is even or odd using bitwise operations.
Hint: Use the & operator to find the remainder of the number divided by 2.
What is the output of the following code snippet?
That's all for this lesson on bitwise operators! Keep practicing to master these powerful tools in your programming arsenal. Happy coding! š”šÆ